Need Help in Form and MySQL

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
szindagi
Forum Newbie
Posts: 6
Joined: Wed Apr 07, 2010 8:24 am

Need Help in Form and MySQL

Post by szindagi »

Assalam o Alaikum,

I've created 2 tables named 'stdinfo' and 'teacherinfo'. In the form there are 2 radio buttons which differentiate between Teacher and Student. I've created a table named 'accountTypeID' which differentiates whether he/she is registering for Teacher Account or Student Account. If 'accoutTypeID' gets value '1' then it means he/she is registering for Teacher Account, if '2' then Student Account.

In both tables there's a field named 'AccoutTypeID'. I want when radio button 'Teacher' is checked so in the table of 'teacherinfo' the field 'AccountTypeID' get value 1 and if radio button 'Student' is checked so in the table of 'stdinfo' the field 'AccountTypeID' get value 2 and according to IDs the whole form data should get store in the same table means if radio button 'Student' is checked then the whole data should get store in 'stdinfo' table and if radio button 'Teacher' is checked then the whole data should get store in 'teacherinfo' table.

Could any one please help me out...............................I need it urgently please!


Regards,
Muhammad Hadi
ell0bo
Forum Commoner
Posts: 79
Joined: Wed Aug 13, 2008 4:15 pm

Re: Need Help in Form and MySQL

Post by ell0bo »

I'm sorry if I come off a bit rude, but this looks like homework.

So have you written the HTML for this year? It sounds like you have the schemas set up, and I'll help you, but I am not going to do it for you.
szindagi
Forum Newbie
Posts: 6
Joined: Wed Apr 07, 2010 8:24 am

Re: Need Help in Form and MySQL

Post by szindagi »

Its OK ..............


Jst tell me how to insert the Account Type ID TO another table FROM 'accoutType' table and the values too.........thnx!
ell0bo
Forum Commoner
Posts: 79
Joined: Wed Aug 13, 2008 4:15 pm

Re: Need Help in Form and MySQL

Post by ell0bo »

Well, from what you explained you wouldn't write out Account_type_id, you would use it to select which table you want to write to.

as for how to connect to as mysql db... http://us2.php.net/manual/en/mysqli.query.php
I'll leave it up to you to google mysql insertion statement.
Arina
Forum Newbie
Posts: 2
Joined: Wed Apr 07, 2010 12:51 pm

Re: Need Help in Form and MySQL

Post by Arina »

Hey,szindagi. I don't specialist in MySQL databasa. Try to use this scheme:
<?php

$con = mysql_connect("localhost","peter","abc123");

if (!$con)

{

die('Could not connect: ' . mysql_error());

}

// Create database

if (mysql_query("CREATE DATABASE my_db",$con))

{

echo "Database created";

}

else

{

echo "Error creating database: " . mysql_error();

}

// Create table

mysql_select_db("my_db", $con);

$sql = "CREATE TABLE Persons

(

FirstName varchar(15),

LastName varchar(15),

Age int

)";

// Execute query

mysql_query($sql,$con);

mysql_close($con);

?>
If I am wrong you may look for here: MySQL Create.
szindagi
Forum Newbie
Posts: 6
Joined: Wed Apr 07, 2010 8:24 am

Re: Need Help in Form and MySQL

Post by szindagi »

Im sending you the code and will explain it..........


$sql = "insert into stdinfo ('StdID','AccTypeID','StdName','StdFName') values ('$_POST[NULL]','$_POST[NULL]','$_POST['StdName']','$_POST['StdFName']')";


The StdID and AccTypeID are kept auto increament. AccTypeID is taken from another table named 'AccType' and the values are:

AccTypeID AccType

1 Teacher
2 Student

My problem is that how to take the AccTypeID from 'AccType' table and how to insert it in 'stdinfo' table.
ell0bo
Forum Commoner
Posts: 79
Joined: Wed Aug 13, 2008 4:15 pm

Re: Need Help in Form and MySQL

Post by ell0bo »

You can do it one of two ways. I would do a select just before the insertion.

$db->query("select AccTypeID from AccType where AccTypeName = ' {$_POST['type']}'");

You didn't say what the second column of your table is, so I presumed it was AccTypeName.
szindagi
Forum Newbie
Posts: 6
Joined: Wed Apr 07, 2010 8:24 am

Re: Need Help in Form and MySQL

Post by szindagi »

Thnx bt I wana insert the AccTypeID from 'AccType' table into my 'stdinfo' table so where I should apply this code? Whether after insert code or before..???
katierosy
Forum Commoner
Posts: 27
Joined: Wed Apr 07, 2010 8:39 am

Re: Need Help in Form and MySQL

Post by katierosy »

If you have 'stdinfo' and 'teacherinfo' tables and both the table have same field structures

Then based on the select option from the submitted form, you will be able to easily store the data in the selected table.

If you are storing selected type from form, in the tables then it is ok and fine. If you need it somewhere you may not store it also , because your tables are different.

If you make one table for student and teachers info then you may use selected type id in the single table to query the database
in this case it is well and good to store in the database.
szindagi
Forum Newbie
Posts: 6
Joined: Wed Apr 07, 2010 8:24 am

Insert Query in Database

Post by szindagi »

HI........


Can we apply two insert queries at a time? Suppose I want to insert data to mysql table through form and want to insert another table's data into the same table.
E.g:
inserting form data to mysql table:

$sql = "insert into stdinfo values ({'[$_POST['name']]'},{'[$_POST['fname']]'},..............)";

Now I want to insert the data of another table in the 'stdinfo' table. Command is:

$sql2 = "insert into stdinfo(AccTypeID) select AccTypeID from AccTypeTable where AccType = 'Teacher' ";

Can we do it to use two insert queries at a time? If yes then what can be the order to use and how ????

Thnx
Regards.
szindagi
Forum Newbie
Posts: 6
Joined: Wed Apr 07, 2010 8:24 am

mysql_insert_id

Post by szindagi »

HI...


I'm getting an id by using mysql_insert_id() query, but I want to insert that id into another table.

How to do that....Guide me plz

Thnx!
Post Reply