Page 1 of 1

Need Help in Form and MySQL

Posted: Wed Apr 07, 2010 8:44 am
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

Re: Need Help in Form and MySQL

Posted: Thu Apr 08, 2010 7:41 am
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.

Re: Need Help in Form and MySQL

Posted: Thu Apr 08, 2010 7:48 am
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!

Re: Need Help in Form and MySQL

Posted: Thu Apr 08, 2010 8:12 am
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.

Re: Need Help in Form and MySQL

Posted: Thu Apr 08, 2010 8:21 am
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.

Re: Need Help in Form and MySQL

Posted: Thu Apr 08, 2010 8:23 am
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.

Re: Need Help in Form and MySQL

Posted: Thu Apr 08, 2010 8:34 am
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.

Re: Need Help in Form and MySQL

Posted: Thu Apr 08, 2010 8:43 am
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..???

Re: Need Help in Form and MySQL

Posted: Thu Apr 08, 2010 10:10 am
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.

Insert Query in Database

Posted: Mon Apr 12, 2010 8:58 am
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.

mysql_insert_id

Posted: Thu Apr 15, 2010 7:28 am
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!