trying to add new record to database – PHP

  php

Q(Question):

Hi,
I have the following code
<?php
//DatabaseConnect();
if (!($dblink = mysql_connect("drt-
sport.com","drtspo_tedted","oreo8157")))
die(" could not connect to mysal");

if (!$dbconn = mysql_select_db("drtspo_zc1",$dblink))
die(" Could not connect to databse ");

$sql="INSERT INTO `orders` (`fname`) VALUES (‘ted’)";
if (!$results=mysql_query($sql,$dblink))
die("<brcould not query database");

print("new item inserted");
?>

The output is always
could not query database.
I’m assuming something is wrong with the sql string?
Is there a way to get PHP to output debug informtion????

-Ted

A(Answer):

yes.

die(mysql_error());

A(Answer):

te*******@gmail.com wrote:

Hi,
I have the following code
<?php
//DatabaseConnect();
if (!($dblink = mysql_connect("drt-
sport.com","drtspo_tedted","oreo8157")))
die(" could not connect to mysal");

if (!$dbconn = mysql_select_db("drtspo_zc1",$dblink))
die(" Could not connect to databse ");

$sql="INSERT INTO `orders` (`fname`) VALUES (‘ted’)";
if (!$results=mysql_query($sql,$dblink))
die("<brcould not query database");

print("new item inserted");
?>

The output is always
could not query database.
I’m assuming something is wrong with the sql string?
Is there a way to get PHP to output debug informtion????

-Ted

My approach is this (usually for more complex queries than that):

1 – Have a side window open and running phpMyAdmin on that database
2 – Put a print statement to print out $sql
3 – Try running that $sql in the sql tab of phpMyAdmin.

That tells me right away what any error is and playing around with the
query in phpMyAdmin to get it to work is usually much faster than first
modifying the php code.

BTW, make sure the "die"s are removed when you go live. Instead, use
ifs and set messages but keep the app moving.

My guess is that either you don’t have field in table orders call fname
or you do not have privilege to write to the database. Just for kicks,
try this:

if (!($results=mysql_query($sql,$dblink)))

Finally, you don’t really need those back-ticks.

A(Answer):

te*******@gmail.com wrote:

Hi,
I have the following code
<?php
//DatabaseConnect();
if (!($dblink = mysql_connect("drt-
sport.com","drtspo_tedted","oreo8157"

)))

die(" could not connect to mysal");

if (!$dbconn = mysql_select_db("drtspo_zc1",$dblink))
die(" Could not connect to databse ");

$sql="INSERT INTO `orders` (`fname`) VALUES (‘ted’)";
if (!$results=mysql_query($sql,$dblink))
die("<brcould not query database");

print("new item inserted");
?>

The output is always
could not query database.
I’m assuming something is wrong with the sql string?
Is there a way to get PHP to output debug informtion????

-Ted

Could the issue be that you’re using a statement that won’t return a
result set with a query statement? In PHP is there an execute() or
update() function that should be used instead?


George Sexton
MH Software, Inc. – Home of Connect Daily Web Calendar
http://www.mhsoftware.com/connectdaily.htm

A(Answer):

George Sexton wrote:

te*******@gmail.com wrote:

>Hi,
I have the following code
<?php
//DatabaseConnect();
if (!($dblink = mysql_connect("drt-
sport.com","drtspo_tedted","oreo8157"

)))

> die(" could not connect to mysal");

if (!$dbconn = mysql_select_db("drtspo_zc1",$dblink))
die(" Could not connect to databse ");

$sql="INSERT INTO `orders` (`fname`) VALUES (‘ted’)";
if (!$results=mysql_query($sql,$dblink))
die("<brcould not query database");

print("new item inserted");
?>

The output is always
could not query database.
I’m assuming something is wrong with the sql string?
Is there a way to get PHP to output debug informtion????

-Ted

Could the issue be that you’re using a statement that won’t return a
result set with a query statement? In PHP is there an execute() or
update() function that should be used instead?

No. mysql_query() always returns non-false for a request which works,
and false for a failure.

Macca’s answer is correct – he needs to use mysql_error() to determine
what the error is.

And Sheldon is correct – you should almost never use die() in production
code. Handle the error and complete your script’s processing.


==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================

A(Answer):

Jerry Stuckle wrote:

George Sexton wrote:

>te*******@gmail.com wrote:

>>Hi,
I have the following code
<?php
//DatabaseConnect();
if (!($dblink = mysql_connect("drt-
sport.com","drtspo_tedted","oreo8157"

)))

>> die(" could not connect to mysal");

if (!$dbconn = mysql_select_db("drtspo_zc1",$dblink))
die(" Could not connect to databse ");

$sql="INSERT INTO `orders` (`fname`) VALUES (‘ted’)";
if (!$results=mysql_query($sql,$dblink))
die("<brcould not query database");

print("new item inserted");
?>

The output is always
could not query database.
I’m assuming something is wrong with the sql string?
Is there a way to get PHP to output debug informtion????

-Ted

Could the issue be that you’re using a statement that won’t return a
result set with a query statement? In PHP is there an execute() or
update() function that should be used instead?

No. mysql_query() always returns non-false for a request which works,
and false for a failure.

Ummmmm. Technically, you are correct in saying "non-false" — but that
statement can be confusing to a newbie. On failure, mysql_query always
returns false. On success, a delete, insert or update returns true,
while a select returns a resource. IOW, if it is something to further
process, it returns a resource.

>
Macca’s answer is correct – he needs to use mysql_error() to determine
what the error is.

And Sheldon is correct – you should almost never use die() in production
code. Handle the error and complete your script’s processing.

A(Answer):

sheldonlg wrote:

Jerry Stuckle wrote:

>George Sexton wrote:

>>te*******@gmail.com wrote:
Hi,
I have the following code
<?php
//DatabaseConnect();
if (!($dblink = mysql_connect("drt-
sport.com","drtspo_tedted","oreo8157"

)))
die(" could not connect to mysal");

if (!$dbconn = mysql_select_db("drtspo_zc1",$dblink))
die(" Could not connect to databse ");

$sql="INSERT INTO `orders` (`fname`) VALUES (‘ted’)";
if (!$results=mysql_query($sql,$dblink))
die("<brcould not query database");

print("new item inserted");
?>

The output is always
could not query database.
I’m assuming something is wrong with the sql string?
Is there a way to get PHP to output debug informtion????

-Ted

Could the issue be that you’re using a statement that won’t return a
result set with a query statement? In PHP is there an execute() or
update() function that should be used instead?

No. mysql_query() always returns non-false for a request which works,
and false for a failure.

Ummmmm. Technically, you are correct in saying "non-false" — but that
statement can be confusing to a newbie. On failure, mysql_query always
returns false. On success, a delete, insert or update returns true,
while a select returns a resource. IOW, if it is something to further
process, it returns a resource.

Which is why I said "non-false" instead of "true".

>>
Macca’s answer is correct – he needs to use mysql_error() to determine
what the error is.

And Sheldon is correct – you should almost never use die() in
production code. Handle the error and complete your script’s processing.


==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================

LEAVE A COMMENT