Log in

View Full Version : MySql error message, please help



iaturner
02-25-2008, 08:23 AM
I am new to programming and typed out an exercise in a book and ended up with the following error message Incorrect integer value "for colum 'id' at row 1. Could someone please explain to me what this means.

tech_support
02-25-2008, 08:46 AM
Stick

iaturner
02-25-2008, 09:04 AM
Thanks heaps. I am working locally on my computer and provide you with the code from the book I'm working with.




<?php
//set up a couple of functions
function doDB(){
global $conn;
//connect to sever and select database; you may need it
$conn = mysql_connect("localhost", "username", "password")
or die(mysql_error());
mysql_select_db("testDB",$conn) or die(mysql_error());
}

function emailChecker($email){
global $conn, $check_result;
//check the email is not already in list
$check = "select id from subscribers where email = '$email'";
$check_result = mysql_query($check,$conn) or die(mysql_error());
}

//determine if they need to see the form or not
if ($_POST[op] != "ds"){
//they do, so create form block
$display_block ="
<form method=POST action=\"$_SERVER[PHP_SELF]\">

<p><strong>Your E-Mail Address:</strong><br>
<input type=text name=\"email\"size=40 maxlength=150>

<p><strong>Action:</strong><br>
<input type=radio name=\"action\" value=\"sub\" checked> subscribe
<input type=radio name=\"action\" value=\"unsub\"> unsubscribe

<input type=\"hidden\" name=\"op\" value=\"ds\">

<p><input type=submit name=\"submit\" value=\"Submit Form\"></p>
</form>";

} else if (($_POST[op] == "ds") && ($_POST[action] == "sub")){
//trying to subscribe; validate email address
if ($_POST[email]==""){
header("Location: manage.php");
exit;
}
//connect to database
doDB();
//check that email is in list
emailChecker($_POST[email]);

//get number of results and do action
if (mysql_num_rows($Check_result) < 1){
//add record
$sql = "insert into subscribers values('','$_POST[email]')";
$result = mysql_query($sql,$conn) or die(mysql_error());
$display_block = "<p>Thanks for signing up!</p>";
} else {
//print failure message
$display_block = "<p>You're already subscribed!</p>";
}
} else if (($_POST[op] == "ds") && ($_POST[action] == "unsub")){
//trying to unsubscribe; validate email address
if ($_POST[email]==""){
header("Location: manage.php");
exit;
}
//connect to database
doDB();
//check email is in list
emailChecker($_POST[email]);

//get number of results and do action
if (mysql_num_rows($check_result) < 1) {
//print failure message
$display_block ="<p>Couldn't find your address!</p>
<p>No action was taken.</p>";
} else {
//unsubscribe the address
$id = mysql_result($check_result, 0, "id");
$sql = "delete from subscribers where id = '$id'";
$result = mysql_query($sql,$conn) or die(mysql_error());
$display_block = "<p>You're unsubscribed!</p>";
}
}
?>
<HTML>
<HEAD>
<TITLE>Subscribe/Unsubscribe</TITLE>
</HEAD>
<BODY>
<h1>Subscribe/Unsubscribe</h1>
<?php echo "$display_block"; ?>
</BODY>
</HTML>


Removed mysql user info and added code tags. Please never post your login info in any code snippet and post your code in [CODE] tags.

boogyman
02-25-2008, 03:27 PM
Welcome to the forums...

when posting code please use the
tags, and also you shouldn't display your user name / password to connect to your database... that is a major security hole. A malicious user could search for this page then hack into your system.


now regarding your question, it appears like you may have not assigned the id field as an integer data type?

the other thing could be


$sql = "insert into subscribers values('','$_POST[email]')";

if you were trying to activate the auto-increment ?? id and just insert the email, you should use a different query. that query is trying to assign an empty string to the that id field. If that field is labeled as an integer correctly and/or it is a primary key, that could be where your error is occurring... try


$sql = "INSERT INTO subscribers(email) VALUES('$_POST[email]')";


Removed note to moderators about removing user info in code snippet