RE: [PHP-GENERAL] Can't Add Record
| From: | Benton Jackson | Date: | Fri, 09 Jun 2000 02:51:36 +0000 |
| Subject: | RE: [PHP-GENERAL] Can't Add Record | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-1362@lists.php.net to get a copy of this message | ||
RAther than just saying "couldn't add record", do this instead:
if ($errno=mysql_errno($db)) {
$error=mysql_error($db);
echo "Error #$errno inserting record: $error<br>\n";
exit;
}
This should give you a much more explanatory message. Your code looks right
otherwise, although I'm not sure if and insert query is supposed to return a
result. I always rely on the error code when doing an insert.
> From: Don Smith [mailto:don@newsvideo.com]
>
> I'm a a loss! The following scripts fails at the
>
> $result = mysql_query($sql);
>
> line.
>
> I've also tried:
>
> $result = mysql_query($sql, $connection);
>
> The required variables are being passed correctly. The script
> gets past the
> 'if' test withut a problem. It connects to the server OK and selects the
> database ok, but the record fails to be written. My table is
> called 'users',
> and I can telnet to the table and enter the exact same INSERT command
> without a problem.
>
> I have the $sql variable echoing back to check that it's being
> formed ok. If
> I haven't changed things by the time you look at it, you can see for
> yourself at http://www.dallasbureau.com/admin_add.php. Log in
> as
> admin with
> pw of hardies.
>
> Anyone?
>
> Many thanks
> Don Smith
>
> <?
>
> if ((!$name) || (!$password) || (!$company) || (!$email)) {
> header("Location:
> http://www.dallasbureau.com/admin_add.php");
> exit;
> }
>
>
> $connection = mysql_connect("localhost", "username",
> "mysqlpw")
> or die ("Couldn't connect to server.");
>
> $db = mysql_select_db("dallasbureau", $connection)
> or die ("Couldn't select database.");
>
> $sql = "INSERT INTO USERS (name,password,company,email)
> VALUES
> ('$name','$password','$company','$email')";
>
> $result = mysql_query($sql);
>
> if (!$result) {
>
> echo "<P>Couldn't add record!";
>
> } else {
>
> echo "
>
> <HTML>
> <HEAD>
> <TITLE></TITLE>
> </HEAD>
> <BODY>
> </BODY>
> </HTML>
> ";
>
> }
> ?>
>
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, e-mail: php-general-unsubscribe@lists.php.net
> For additional commands, e-mail: php-general-help@lists.php.net
> To contact the list administrators, e-mail: php-list-admin@lists.php.net