trying to add new record to database 
November 17th, 2008, 10:45 PM
| | | |
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 | 
November 17th, 2008, 11:05 PM
| | | | re: trying to add new record to database
yes.
die(mysql_error()); | 
November 18th, 2008, 12:05 AM
| | | | re: trying to add new record to database tedpottel@gmail.com wrote: Quote:
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. | 
November 18th, 2008, 04:45 AM
| | | | re: trying to add new record to database tedpottel@gmail.com wrote: Quote:
Hi,
I have the following code
<?php
//DatabaseConnect();
if (!($dblink = mysql_connect("drt-
sport.com","drtspo_tedted","oreo8157"
| ))) Quote:
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 | 
November 18th, 2008, 12:05 PM
| | | | re: trying to add new record to database
George Sexton wrote: Quote: tedpottel@gmail.com wrote: Quote:
>Hi,
>I have the following code
><?php
> //DatabaseConnect();
> if (!($dblink = mysql_connect("drt-
>sport.com","drtspo_tedted","oreo8157"
| >
))) Quote:
> 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. jstucklex@attglobal.net
================== | 
November 18th, 2008, 05:25 PM
| | | | re: trying to add new record to database
Jerry Stuckle wrote: Quote:
George Sexton wrote: Quote:
>tedpottel@gmail.com wrote: Quote:
>>Hi,
>>I have the following code
>><?php
>> //DatabaseConnect();
>> if (!($dblink = mysql_connect("drt-
>>sport.com","drtspo_tedted","oreo8157"
| >>
>))) Quote:
>> 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. Quote:
>
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.
>
| | 
November 18th, 2008, 06:55 PM
| | | | re: trying to add new record to database
sheldonlg wrote: Quote:
Jerry Stuckle wrote: Quote:
>George Sexton wrote: Quote:
>>tedpottel@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". Quote: Quote:
>>
>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. jstucklex@attglobal.net
================== |  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 225,689 network members.
|