Connecting Tech Pros Worldwide Help | Site Map

trying to add new record to database

  #1  
Old November 17th, 2008, 10:45 PM
tedpottel@gmail.com
Guest
 
Posts: n/a
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
  #2  
Old November 17th, 2008, 11:05 PM
macca
Guest
 
Posts: n/a

re: trying to add new record to database


yes.

die(mysql_error());
  #3  
Old November 18th, 2008, 12:05 AM
sheldonlg
Guest
 
Posts: n/a

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.
  #4  
Old November 18th, 2008, 04:45 AM
George Sexton
Guest
 
Posts: n/a

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
  #5  
Old November 18th, 2008, 12:05 PM
Jerry Stuckle
Guest
 
Posts: n/a

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
==================
  #6  
Old November 18th, 2008, 05:25 PM
sheldonlg
Guest
 
Posts: n/a

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.
>
  #7  
Old November 18th, 2008, 06:55 PM
Jerry Stuckle
Guest
 
Posts: n/a

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
==================
Closed Thread


Similar Threads
Thread Thread Starter Forum Replies Last Post
Add new record to database Marko answers 1 November 29th, 2006 01:15 PM
Add new record to database Marko answers 1 November 29th, 2006 01:15 PM
Add new record to database Marko answers 1 November 29th, 2006 01:15 PM
error when trying to add record to Access 2000 db Frances answers 0 November 12th, 2005 03:22 PM