doforumda Posted April 24, 2009 Share Posted April 24, 2009 hi again i have a code with insert query. yesterday one of the member in this forum helped in solving it and it worked but today when i am using that same technique with other code it doesnt work. the code is below the problem with this code is it doesnt insert data into mysql database. <?php $firstName=(isset($_POST['firstName']))?$_POST['firstName']:""; $lastName=(isset($_POST['lastName']))?$_POST['lastName']:""; $region=(isset($_POST['region']))?$_POST['region']:""; $ssn=(isset($_POST['ss#']))?$_POST['ss#']:""; $db = mysql_connect("localhost","username","password"); mysql_select_db("database"); //$query = "select * from statusValues"; //$result = mysql_query($query); if(isset($function) && $function=="add") { $sqlInsert = "insert into statusvalues(firstName,lastName,region,ss#) values('".$firstName."','".$lastName."','".$region."','".$ssn."')"; $result = mysql_query($sqlInsert) or die(mysql_error()); } ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Untitled Document</title> </head> <body> <form id="form1" name="form1" method="post" action="index1.php?function=add"> <label>First Name: <input type="text" name="firstName" id="firstName" /> </label> <p> <label>Last Name: <input type="text" name="lastName" id="lastName" /> </label> </p> <p> <?php $regSQL = "select * from regions"; $regSQLResult = mysql_query($regSQL); ?> <label>Select Region Number: <select name="region" id="region"> <?php while($myrow = mysql_fetch_array($regSQLResult)) { ?> <option value="<?php echo $myrow['regionNumber']; ?>"><?php echo $myrow['regionNumber']."-".$myrow['regionName']; ?></option> <?php } ?> </select> </label> </p> <p> <label>Enter SSN No Dashes <input type="text" name="ssn" id="ssn" /> </label> </p> <p> <label> <input type="submit" name="Submit" id="Submit" value="ok" /> </label> </p> </form> </body> </html> Quote Link to comment Share on other sites More sharing options...
Yesideez Posted April 24, 2009 Share Posted April 24, 2009 MySQL INSERT has two different ways you can insert data. Method 1: INSERT INTO `table` (`field1`,`field2`,`field3`) VALUES ('val1','val2','val3') Method 2: INSERT INTO `table` SET `field1`='val1',`field2`='val2',`field3`='val3' The second method makes it easier to see what's being assigned to what. Although looking at your code it may be the # in the field name - try this: $sqlInsert = "insert into `statusvalues` (`firstName`,`lastName`,`region`,`ss#`) values ('".$firstName."','".$lastName."','".$region."','".$ssn."')"; I added two spaces and small "ticks" around table and field names. Quote Link to comment Share on other sites More sharing options...
doforumda Posted April 24, 2009 Author Share Posted April 24, 2009 yesideez it still doesnt work Quote Link to comment Share on other sites More sharing options...
Yesideez Posted April 24, 2009 Share Posted April 24, 2009 OK. Right after that line I just re-formatted insert this line: echo $sqlInsert; Then post the query back if you can't see anything wrong with it. Quote Link to comment Share on other sites More sharing options...
doforumda Posted April 24, 2009 Author Share Posted April 24, 2009 i am not getting anything back Quote Link to comment Share on other sites More sharing options...
Yesideez Posted April 24, 2009 Share Posted April 24, 2009 Then you need to look at the if() statement before it as it's equating to false and your query is never being called. Quote Link to comment Share on other sites More sharing options...
doforumda Posted April 24, 2009 Author Share Posted April 24, 2009 yes i also think there is a problem with if() statment but how can i know whats the problem Quote Link to comment Share on other sites More sharing options...
Yesideez Posted April 24, 2009 Share Posted April 24, 2009 I can't see $function getting a value AT ALL! This isset() will always equate to false as $function is never being assigned a value let along "add". You need to check where $function comes into this. Quote Link to comment Share on other sites More sharing options...
doforumda Posted April 24, 2009 Author Share Posted April 24, 2009 it is assigned in this line <form id="form1" name="form1" method="post" action="index1.php?function=add"> Quote Link to comment Share on other sites More sharing options...
doforumda Posted April 24, 2009 Author Share Posted April 24, 2009 when i comment if() statement out <?php $firstName=(isset($_POST['firstName']))?$_POST['firstName']:""; $lastName=(isset($_POST['lastName']))?$_POST['lastName']:""; $region=(isset($_POST['region']))?$_POST['region']:""; $ssn=(isset($_POST['ssn']))?$_POST['ssn']:""; $db = mysql_connect("localhost","root","123456"); mysql_select_db("cartoonsmart"); //$query = "select * from statusValues"; //$result = mysql_query($query); //if(isset($function) && $function=="true") //{ $sqlInsert = "insert into 'statusvalues' ('firstName','lastName','region','ssn') values ('".$firstName."','".$lastName."','".$region."','".$ssn."')"; echo $sqlInsert; //$sqlInsert = "insert into statusvalues set 'firstName'='".$firstName."','lastName'='".$lastName."','region'='".$region."','ss#'='".$ssn."'"; $result = mysql_query($sqlInsert) or die(mysql_error()); //} ?> then it displays this error insert into 'statusvalues' ('firstName','lastName','region','ssn') values ('','','','')You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''statusvalues' ('firstName','lastName','region','ssn') values ('','',' at line 1 Quote Link to comment Share on other sites More sharing options...
Yesideez Posted April 24, 2009 Share Posted April 24, 2009 I don't think MySQL likes the # in the field name but not 100% sure on that as I only stick to a-z, A-Z and underscore in my field names. Quote Link to comment Share on other sites More sharing options...
doforumda Posted April 24, 2009 Author Share Posted April 24, 2009 if you see the code then you will see i remove # sign Quote Link to comment Share on other sites More sharing options...
doforumda Posted April 24, 2009 Author Share Posted April 24, 2009 my problem is still unresolved anyone out there who can solve my problem Quote Link to comment Share on other sites More sharing options...
radi8 Posted April 24, 2009 Share Posted April 24, 2009 I do not think that the values are being retrieved from the post. do the following and repost the results: <?php $firstName=(isset($_POST['firstName']))?$_POST['firstName']:""; $lastName=(isset($_POST['lastName']))?$_POST['lastName']:""; $region=(isset($_POST['region']))?$_POST['region']:""; $ssn=(isset($_POST['ssn']))?$_POST['ssn']:""; echo $firstName."<br>"; echo $lastName."<br>"; echo $region."<br>"; echo $ssn."<br>"; // be sure it is a dummy SSN ?> Quote Link to comment Share on other sites More sharing options...
doforumda Posted April 24, 2009 Author Share Posted April 24, 2009 well it does echoing all the variables using this way. post does work Quote Link to comment Share on other sites More sharing options...
doforumda Posted April 24, 2009 Author Share Posted April 24, 2009 the problem is with if statement it is not becoming true Quote Link to comment Share on other sites More sharing options...
doforumda Posted April 25, 2009 Author Share Posted April 25, 2009 hi i made some changes now i am getting this error You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''fname','lname',region,ssn) values ('zafar','saleem',2,77785555552112' at line 1 my code is <?php $firstName=(isset($_POST['firstName']))?$_POST['firstName']:""; $lastName=(isset($_POST['lastName']))?$_POST['lastName']:""; $region=(isset($_POST['region']))?$_POST['region']:""; $ssn=(isset($_POST['ssn']))?$_POST['ssn']:""; $db = mysql_connect("localhost","username","password"); mysql_select_db("db"); //$query = "select * from statusValues"; //$result = mysql_query($query); if(isset($function) && $function=="true") { $sqlInsert = "insert into statusvalues2 ('fname','lname',region,ssn) values ('".$fname."','".$lname."',".$rnumber.",".$snumber.")"; //echo $sqlInsert; /*$sqlInsert = "insert into statusvalues set 'firstName'='".$firstName."','lastName'='".$lastName."','region'='".$region."','ssn'='".$ssn."'";*/ $result = mysql_query($sqlInsert) or die(mysql_error()); } ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Untitled Document</title> </head> <body> <form id="form1" name="form1" method="post" action="index1.php?function=true"> <label>First Name: <input type="text" name="fname" id="fname" /> </label> <p> <label>Last Name: <input type="text" name="lname" id="lname" /> </label> </p> <p> <?php $regSQL = "select * from regions"; $regSQLResult = mysql_query($regSQL); ?> <label>Select Region Number: <select name="rnumber" id="rnumber"> <?php while($myrow = mysql_fetch_array($regSQLResult)) { ?> <option value="<?php echo $myrow['regionNumber']; ?>"><?php echo $myrow['regionNumber']."-".$myrow['regionName']; ?></option> <?php } ?> </select> </label> </p> <p> <label>Enter SSN No Dashes <input type="text" name="snumber" id="snumber" /> </label> </p> <p> <label> <input type="submit" name="Submit" id="Submit" value="ok" /> </label> </p> </form> </body> </html> Quote Link to comment Share on other sites More sharing options...
doforumda Posted April 25, 2009 Author Share Posted April 25, 2009 anyone who can solve my problem Quote Link to comment Share on other sites More sharing options...
trq Posted April 25, 2009 Share Posted April 25, 2009 $sqlInsert = "insert into statusvalues2 (fname,lname,region,ssn) values ('$fname','$lname','$rnumber','$snumber')"; Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.