Prepared Statements Insert Problem
| From: | Ethan Rosenberg, PhD | Date: | Sun, 02 Sep 2012 04:45:09 +0000 |
| Subject: | Prepared Statements Insert Problem | ||
| Groups: | php.db | ||
| Request: | Send a blank email to php-db+get-48447@lists.php.net to get a copy of this message | ||
Dear List -
I wish to accomplish the following with prepared statements:
FYI -
The Database:
mysql> describe Intake3;
+--------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |+--------+-------------+------+-----+---------+-------+
| Site | varchar(6) | NO | PRI | | | | MedRec | int(6) | NO | PRI | NULL | | | Fname | varchar(15) | YES | | NULL | | | Lname | varchar(30) | YES | | NULL | | | Phone | varchar(30) | YES | | NULL | | | Height | int(4) | YES | | NULL | | | Sex | char(7) | YES | | NULL | | | Hx | text | YES | | NULL | | | Bday | date | YES | | NULL | | | Age | int(3) | YES | | NULL | |+--------+-------------+------+-----+---------+-------+ The connection to the database was successful The variables: $fptr1 = fopen("/home/ethan/PHP/HRecnumSite", "r+"); fscanf($fptr1,"%d %s",$MedRec, $Site); $_POST['MedRec'] = $MedRec; $_POST['Site'] = $Site; $fname = $_POST['Fname']; $lname = $_POST['Lname']; $phone = $_POST['Phone']; $hgt = $_POST['Height'];
$sx = $_POST['Sex']; $hx = $_POST['Hx'];$bday = $_POST['Bday']; $age = $_POST['Age']; if($sx==1)$_POST['Sex'] = 'F'; if($sx==0)$_POST['Sex'] = 'M'; Statement to convert: $sql1 = "INSERT INTO Intake3(Site, MedRec, Fname, Lname, Phone, Height, Sex, Hx, Bday, Age) VALUES('$Site', $MedRec, '$fname', '$lname', '$phone', $hgt, '$sx', '$hx', '$bday', '$age')"; My attempt [which failed] $stmt = mysqli_stmt_init($cxn);
if($stmt = mysqli_stmt_prepare($stmt, "INSERT INTO Intake3 (Site, MedRec, Fname, Lname, Phone, Height, Sex, Hx, Bday, Age) VALUES(?,?,?,?,?,?,?,?,?,?")!=0)
{
print_r($stmt);
mysqli_stmt_bind_param($stmt, 'sisssisssi', $Site, $MedRec, $fname, $lname, $phone, $hgt, $sx, $hx, $bday, $age);
mysqli_execute($stmt);
mysqli_stmt_bind_result($stmt, $Site, $MedRec, $fname, $lname, $phone, $hgt, $sx, $hx, $bday, $age);
echo $stmt;
mysqli_stmt_fetch($stmt);
mysqli_stmt_close($stmt);
}
else
echo "Ouch";
Regrettably, all I see on the monitor is "ouch"!!
Help and advice, please.
Ethan Rosenberg