Welcome! Log In Create A New Profile

Advanced

HELP!!!!! Code not inserting data into my database

Posted by "thabzzz" 
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
HELP!!!!! Code not inserting data into my database
October 19, 2011 08:16AM
Hi Everyone,

I need some help, I wrote a small code to insert to user input into my database. I think the code works because im not getting any errors and the user input is stored. But it is not stored into my database. I tried debugging the code but i cant see wats wrong??

Language: PHP
if($db_found) {   $SQL = "INSERT INTO Name(Naam,van,gen,nick) VALUES (';$_POST[FirstName]';,';$_POST[LastName]';,';$_POST ]';,';$_POST[sex]';,';$_POST[ID]';)"; $result = mysql_query($SQL); mysql_close($db_handle); } else print "Database Not Found";


Thanks
Re: HELP!!!!! Code not inserting data into my database
October 19, 2011 09:26AM
For starters check your syntax this '$_POST ]' does not look very right......
Re: HELP!!!!! Code not inserting data into my database
October 19, 2011 09:50AM
Hi Des

I saw that syntax here and if it was a syntax error i think i was goining to get an error but im not getting any error.
avatar Re: HELP!!!!! Code not inserting data into my database
October 19, 2011 01:47PM
Hi thabzzz,

Print out the SQL code and run it in phpMyAdmin an see if it gives errors, the error desmond pointed out shouldn't be a problem it should insert value (Array ])

You can also try:
Language: PHP
$result = mysql_query($SQL) or die(mysql_error());

student no: 77315138
Re: HELP!!!!! Code not inserting data into my database
October 19, 2011 05:35PM
Thanks Riaz, i included your code and got this error:
"Error: Column count doesn't match value count at row 1"

Atleast now I have something to work on.
avatar Re: HELP!!!!! Code not inserting data into my database
October 19, 2011 06:28PM
i think cause its trying to insert five values when only 4 colums exist

btw is this for your practical?
Re: HELP!!!!! Code not inserting data into my database
October 19, 2011 06:53PM
Nope its not my prac code. I was just playing around with the registration page. I just wanted to see if some of the code will work , I did not want to code everything at once and then run it later.
avatar HELP!!!!! Code not inserting data into my database
October 19, 2011 08:30PM
I'm using the vim text editor in a terminal. When I copy and paste your code over and add php tags, it displays the code in color which is very helpful for me in displaying where the problem is. You should try one of the text editors that displays your code in color.

This is how I see your code:
<div style="color: white; background: black;"><pre>
<span style="color: fuchsia;">&lt;?php</span>
<span style="color: orange;">if</span><span style="color: fuchsia;">(</span><span style="color: orange;">$</span><span style="color: aqua;">db_found</span><span style="color: fuchsia;">winking smiley</span>
<span style="color: fuchsia;">{</span>

<span style="color: orange;">$</span><span style="color: aqua;">SQL</span> <span style="color: orange;">=</span> "<span style="color: red;">INSERT INTO Name(Naam,van,gen,nick) VALUES
('</span><span style="color: orange;">$</span><span style="color: aqua;">_POST</span><span style="color: red;">[FirstName]','</span><span style="color: orange;">$</span><span style="color: aqua;">_POST</span><span style="color: red;">[LastName]','</span><span style="color: orange;">$</span><span style="color: aqua;">_POST</span> <span style="color: red;">]','</span><span style="color: orange;">$</span><span style="color: aqua;">_POST</span><span style="color: red;">[sex]','</span><span style="color: orange;">$</span><span style="color: aqua;">_POST</span><span style="color: red;">[ID]'winking smiley</span>";
<span style="color: orange;">$</span><span style="color: aqua;">result</span> <span style="color: orange;">=</span> <span style="color: aqua;">mysql_query</span><span style="color: fuchsia;">(</span><span style="color: orange;">$</span><span style="color: aqua;">SQL</span><span style="color: fuchsia;">winking smiley</span>;
<span style="color: aqua;">mysql_close</span><span style="color: fuchsia;">(</span><span style="color: orange;">$</span><span style="color: aqua;">db_handle</span><span style="color: fuchsia;">winking smiley</span>;
<span style="color: fuchsia;">}</span>
<span style="color: orange;">else</span>
<span style="color: fuchsia;">print</span> "<span style="color: red;">Database Not Found</span>";
<span style="color: fuchsia;">?&gt;</span>
</pre></div>

I have identified part of the problem just by looking at the color of the text. The square brackets should be purple (in my case). The text between the square brackets should be in single quotes. When you inserted $_POST variables in a double quote statement, you should enclose the variables with curly braces.

Here is how the modifications will look:
<div style="color: white; background: black;"><pre>
<span style="color: fuchsia;">&lt;?php</span>
<span style="color: orange;">if</span><span style="color: fuchsia;">(</span><span style="color: orange;">$</span><span style="color: aqua;">db_found</span><span style="color: fuchsia;">winking smiley</span>
<span style="color: fuchsia;">{</span>
<span style="color: orange;">$</span><span style="color: aqua;">SQL</span> <span style="color: orange;">=</span> "<span style="color: red;">INSERT INTO Name(Naam,van,gen,nick) VALUES
('</span><span style="color: orange;"><span style="color: fuchsia;">{</span>$</span><span style="color: aqua;">_POST</span><span style="color: fuchsia;">[</span>'<span style="color: red;">FirstName</span>'<span style="color: fuchsia;">]}</span><span style="color: red;">','</span><span style="color: fuchsia;">{</span><span style="color: orange;">$</span><span style="color: aqua;">_POST</span><span style="color: fuchsia;">[</span>'<span style="color: red;">LastName</span>'<span style="color: fuchsia;">]}</span><span style="color: red;">','</span><span style="color: fuchsia;">{</span><span style="color: orange;">$</span><span style="color: aqua;">_POST</span><span style="color: fuchsia;">[</span>'<span style="color: red;">unamed_index</span>'<span style="color: fuchsia;">]}</span><span style="color: red;">','</span><span style="color: fuchsia;">{</span><span style="color: orange;">$</span><span style="color: aqua;">_POST</span><span style="color: fuchsia;">[</span>'<span style="color: red;">sex</span>'<span style="color: fuchsia;">]}</span><span style="color: red;">','</span><span style="color: fuchsia;">{</span><span style="color: orange;">$</span><span style="color: aqua;">_POST</span><span style="color: fuchsia;">[</span>'<span style="color: red;">ID</span>'<span style="color: fuchsia;">]}</span><span style="color: red;">'winking smiley</span>";
<span style="color: orange;">$</span><span style="color: aqua;">result</span> <span style="color: orange;">=</span> <span style="color: aqua;">mysql_query</span><span style="color: fuchsia;">(</span><span style="color: orange;">$</span><span style="color: aqua;">SQL</span><span style="color: fuchsia;">winking smiley</span>;
<span style="color: aqua;">mysql_close</span><span style="color: fuchsia;">(</span><span style="color: orange;">$</span><span style="color: aqua;">db_handle</span><span style="color: fuchsia;">winking smiley</span>;
<span style="color: fuchsia;">}</span>
<span style="color: orange;">else</span>
<span style="color: fuchsia;">print</span> "<span style="color: red;">Database Not Found</span>";
<span style="color: fuchsia;">?&gt;</span>
</pre></div>
Re: HELP!!!!! Code not inserting data into my database
October 19, 2011 09:10PM
Thanks Ash, that might be another problem. PeterJ was right I had 5 values instead of 4 because I had 4 fields. I removed I'd value and it inserted the other values in the database.

Thanks guys
avatar Re: HELP!!!!! Code not inserting data into my database
October 20, 2011 10:55AM
Quote
Ash
You should try one of the text editors that displays your code in color.

Agreed. Was using Notepad++ but recently discovered Bluefish which I find more user-friendly in its colour coding for some or other reason.
avatar Re: HELP!!!!! Code not inserting data into my database
October 20, 2011 06:18PM
Im using phpdesigner
Will have a look at the others recommened
Re: HELP!!!!! Code not inserting data into my database
October 21, 2011 09:29AM
I am also using phpdesigner. What are the other one highly recommended? And while we are at it, what is CakePHP (definetely not cookies here)smile
avatar
Mac
Re: HELP!!!!! Code not inserting data into my database
October 21, 2011 10:06AM
CakePHP is a framework, i.e. it helps you to code faster. But takes you longer to learn. Leave frameworks until you have more experience with PHP.
Sorry, only registered users may post in this forum.

Click here to login