Jump to content

help


doforumda

Recommended Posts

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>

Link to comment
https://forums.phpfreaks.com/topic/155474-help/
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/155474-help/#findComment-818118
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/155474-help/#findComment-818146
Share on other sites

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

?>

Link to comment
https://forums.phpfreaks.com/topic/155474-help/#findComment-818293
Share on other sites

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>

Link to comment
https://forums.phpfreaks.com/topic/155474-help/#findComment-818790
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.