Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Help With Inserting Into a Database
Old 09-10-2007, 10:24 PM Help With Inserting Into a Database
cbeaudin's Avatar
Extreme Talker

Posts: 158
Name: cbeaudin
Location: Proud to be Canadian
Trades: 1
I'm using the following code to insert a row into mysql database. It inserts the information but inserts three rows instead of one. Maybe I'm just overly tired but i cant seem to figure out why... Can anyone help me out?

PHP Code:
if ($error>0)
    {
    echo 
"<p>This script has been terminated due to one or more errors. Please read the errors above and try again</p>";
    include 
"common/end.php";
    die();
    }
    else
    {
    
$name2 htmlspecialchars($name);
    
$message2 htmlspecialchars($message);
    
$today=date("U");
    
    
$addmessage "INSERT INTO messages (id, name, date, message) VALUES ('', '$name2', '', '$message2')";
    
$addmessage2 mysql_query($addmessage) or DIE ("Could not add message");
    echo 
"<p>Message was successfully added to the database. You should be able to see it instantly.</p>";
    } 
__________________
- cbeaudin
cbeaudin is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 09-10-2007, 10:43 PM Re: Help With Inserting Into a Database
Experienced Talker

Posts: 44
Name: Kuldeep Sahi
Trades: 0
Are you getting the message Message was successfully added to the database once or 3 times.

Since you are not entering the values of id & date, you can simplify the query to:

$addmessage = "INSERT INTO messages (name, message) VALUES ('$name2', '$message2')";

__________________

Please login or register to view this content. Registration is FREE
,
Please login or register to view this content. Registration is FREE
,
Please login or register to view this content. Registration is FREE
Kuldeep2195 is offline
Reply With Quote
View Public Profile Visit Kuldeep2195's homepage!
 
Old 09-10-2007, 10:49 PM Re: Help With Inserting Into a Database
cbeaudin's Avatar
Extreme Talker

Posts: 158
Name: cbeaudin
Location: Proud to be Canadian
Trades: 1
I will be adding date soon, and that is true, i dont need id, i will take that out.

The message Message was successfully added to the database only comes up once.
__________________
- cbeaudin
cbeaudin is offline
Reply With Quote
View Public Profile
 
Old 09-11-2007, 10:43 AM Re: Help With Inserting Into a Database
Experienced Talker

Posts: 44
Name: Kuldeep Sahi
Trades: 0
You may echo $addmessage and run its contents in
phpmyadmin.

You may be able to infer the problem from this or
by studying the contents of the two additional rows.
__________________

Please login or register to view this content. Registration is FREE
,
Please login or register to view this content. Registration is FREE
,
Please login or register to view this content. Registration is FREE
Kuldeep2195 is offline
Reply With Quote
View Public Profile Visit Kuldeep2195's homepage!
 
Old 09-11-2007, 11:41 AM Re: Help With Inserting Into a Database
cbeaudin's Avatar
Extreme Talker

Posts: 158
Name: cbeaudin
Location: Proud to be Canadian
Trades: 1
If i echo the SQL and run it in PHPMyAdmin, it only adds one row.

Strangely enough though if i remove id from the script it only adds one row from the script.

My problem is solved and i do not need to re-add id, however I'm curious to know if anyone else knows why that happened?
__________________
- cbeaudin
cbeaudin is offline
Reply With Quote
View Public Profile
 
Old 09-11-2007, 03:34 PM Re: Help With Inserting Into a Database
metho's Avatar
Ultra Talker

Posts: 481
Location: Gold Coast - Brisbane QLD, Australia
Trades: 0
Field 'id' is more than likely an autonum. If this is the case, you don't need to include it in your query.

You may want to add one additional level of error checking though:

PHP Code:
if ($error>0)
    {
    echo 
"<p>This script has been terminated due to one or more errors. Please read the errors above and try again</p>";
    include 
"common/end.php";
    die();
    }
    else
    {
    
$name2 htmlspecialchars($name);
    
$message2 htmlspecialchars($message);
    
$today=date("U");
    
    
$addmessage "INSERT INTO messages (name, date, message) VALUES ('$name2', '', '$message2')";
    
$insertQuery mysql_query($addmessage);
    
$insertOK mysql_affected_rows();
    if(
$insertOK != 1) echo "<p>Query failed; message was not added.</p>";
    else echo 
"<p>Message was successfully added to the database. You should be able to see it instantly.</p>";
    } 
mysql_affected_rows() will only work if 'id' is autonum and not tinyint.
__________________
I do
Please login or register to view this content. Registration is FREE
based.
Spend a lot of time in
Please login or register to view this content. Registration is FREE
.
And
Please login or register to view this content. Registration is FREE
chews up the rest.
metho is offline
Reply With Quote
View Public Profile Visit metho's homepage!
 
Reply     « Reply to Help With Inserting Into a Database
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.64820 seconds with 12 queries