Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

My row won't insert - but no error msg.

Status
Not open for further replies.

dpdoug

Programmer
Joined
Nov 27, 2002
Messages
455
Location
US
The following code is supposed to insert a row into table1, but the output I get for mysql_affected_rows() is -1 and in fact, the row is not inserted. I'm not getting any error messages. What am I doing wrong?

<?PHP

$first = $_POST[&quot;first&quot;];
$last = $_POST[&quot;last&quot;];
$addr = $_POST[&quot;Addr&quot;];
$city = $_POST[&quot;city&quot;];
$state = $_POST[&quot;state&quot;];
$zip = $_POST[&quot;zip&quot;];
$phone = $_POST[&quot;phone&quot;];

$db = mysql_connect(&quot;Dev-1&quot;, &quot;root&quot;, &quot;&quot;);
mysql_select_db(&quot;db1&quot;, $db);
$sql = &quot;Insert into table1 (First, Last, Addr, City, State, zip, Phone)
Values ($first, $last, $addr, $city, $state, $zip, $phone)&quot;;
mysql_query($sql, $db);
printf (&quot;Updated records: %d\n&quot;, mysql_affected_rows());
mysql_query(&quot;COMMIT&quot;);
?>
 
assuming that most of the vars are text in the db, you need the quotes

$sql = &quot;Insert into table1 (First, Last, Addr, City, State, zip, Phone)
Values ('$first', '$last', '$addr', '$city', '$state', '$zip',
'$phone')&quot;;

Bastien

cat, the other other white meat
 
Thanks. That worked for me!
 
As a more general solution to the question, when used with an update or insert query, mysql_query() will return a TRUE or FALSE, depending on whether the query was successful.

You can output the return of the function of mysql_error() when the return is FALSE.

Want the best answers? Ask the best questions: TANSTAAFL!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top