Welcome! Log In Create A New Profile

Advanced

inserting data into database?

Posted by 77686160 
Announcements Last Post
Announcement SoC Curricula 09/30/2017 01:08PM
Announcement Demarcation or scoping of examinations and assessment 02/13/2017 07:59AM
Announcement School of Computing Short Learning Programmes 11/24/2014 08:37AM
Announcement Unisa contact information 07/28/2011 01:28PM
inserting data into database?
October 26, 2012 10:42AM
Hi, everyone.
I'm busy trying to do the student registration part where the info has to be inserted into the database. I have used below code do do the insert, but even though I get message that its successful, nothing happens in the database. Any suggestions?

Language: PHP
<?PHP $db_handle = mysql_connect("localhost", "root" , "") or die("Could not connect: " . mysql_error()); mysql_select_db(';registration';, $db_handle) or die(mysql_error());   $studentsql = "SELECT * FROM student";   $result = mysql_query($studentsql) or die("Invalid query: " . mysql_error());     $studentsql="INSERT INTO student (sname, init, fname, title, msname, dob, gen, lang, idno, telh, telw, cel, fax, email, address) VALUES (';$_POST[sname]';,';$_POST[init]';,';$_POST[fname]';,';$_POST[title]';,';$_POST[msname]'; ,';$_POST[dob]';,';$_POST[gen]';, ';$_POST[lang]';,';$_POST[idno]';,';$_POST[telh]';,';$_POST[telw]';,';$_POST[cel]';,';$_POST[fax]';,';$_POST[email]';,';$_POST[address]';)";     if($result){ echo("<br>Data input is successfull"); } else{ echo("<br>Data input has failed"); }     ?>
Re: inserting data into database?
October 26, 2012 10:57AM
I think you could have a field mis-match here which results in nothing being inserted to the db, isn't there supposed to be a sno field as well? Also if you have primary keys that aren't auto increment and you're not modfying that field it could prevent more than one record from being inserted in the db ... Hope you find the problem!
avatar Re: inserting data into database?
October 26, 2012 11:13AM
@77686160 you have 2 variables called "$studentsql" the first one you're running your query on, which is your mysql SELECT query (you actually don't have to do this one)
The second one is where you're actualy inserting the data, on this one you're not running your query.
Language: PHP
$studentsql="INSERT INTO student (sname, init, fname, title, msname, dob, gen, lang, idno, telh, telw, cel, fax, email, address) VALUES (';$_POST[sname]';,';$_POST[init]';,';$_POST[fname]';,';$_POST[title]';,';$_POST[msname]'; ,';$_POST[dob]';,';$_POST[gen]';, ';$_POST[lang]';,';$_POST[idno]';,';$_POST[telh]';,';$_POST[telw]';,';$_POST[cel]';,';$_POST[fax]';,';$_POST[email]';,';$_POST[address]';)";
your check for $result is valid because you're checking the $result/query for your SELECT,meaning all you're doing is getting info(all the records) from the database:
Language: PHP
studentsql = "SELECT * FROM student";   $result = mysql_query($studentsql) or die("Invalid query: " . mysql_error());

so after your 2nd $studentsql you should run your query like below, this will then do the INSERT into your db part.

Language: PHP
$studentsql="INSERT INTO student (sname, init, fname, title, msname, dob, gen, lang, idno, telh, telw, cel, fax, email, address) VALUES (';$_POST[sname]';,';$_POST[init]';,';$_POST[fname]';,';$_POST[title]';,';$_POST[msname]'; ,';$_POST[dob]';,';$_POST[gen]';, ';$_POST[lang]';,';$_POST[idno]';,';$_POST[telh]';,';$_POST[telw]';,';$_POST[cel]';,';$_POST[fax]';,';$_POST[email]';,';$_POST[address]';)";   $result = mysql_query($studentsql) or die("Invalid query: " . mysql_error());
Re: inserting data into database?
October 26, 2012 11:16AM
That's right. I made the same mistake a few times as well.
Anonymous User
Re: inserting data into database?
October 26, 2012 11:17AM
your single quotes are also in the wrong place should be inside these [] as your input field will be inserted
Language: PHP
';$_POST[init]';
should be
Language: PHP
$_POST[';init';]
Re: inserting data into database?
October 26, 2012 11:20AM
Hi, Lance
Thanks a mil. I will try it just now and see if it works.

I do have an sno field in the student table, but as it is suppose to auto increment I am not including it in the insert page. I will look into it soon though as I know it must be inserted into the course_student table as well. Not getting my head around everything.

Jenny
avatar Re: inserting data into database?
October 26, 2012 11:25AM
cooliothumbs up. if your sno is set to auto increment then you dont have to specify it in your INSERT statement.
Re: inserting data into database?
October 26, 2012 11:42AM
@77686160, (DO NOT FORGET to escape strings to prevent SQL Injection Attacks)....Never insert data into your database straight from the user inputs without doing some security checks.
Sorry, only registered users may post in this forum.

Click here to login