/    Sign up×
Community /Pin to ProfileBookmark

Error Scripting

Hi, i have created a basic script to write to a database and a few errors happen:
1st it puts it into the database twice :s i rly dont know y and im not refreshing.
2nd i need to to be able to catch the error message the sql brings back if there is an error.

Here is the source code can someone please look at tell me whats wrong lol

[code=php]
<?php
$conn = mysql_connect(“localhost”, “k0m0d0”, “ragnarok”);
mysql_select_db(“ebay”,$conn);
$sql = “insert into nametable values(‘$_POST[name1]’, ‘$_POST[name2],’, ‘$_POST[name3]’, ‘$_POST[name4]’)”;
$result= mysql_query($sql, $conn);
if (mysql_query($sql, $conn)) {
echo “success! o/”;
} else {
echo “no works”;
}
?>
[/code]

to post a comment
PHP

3 Comments(s)

Copy linkTweet thisAlerts:
@AdamGundryAug 13.2004 — The reason the insert occurs twice is that you are calling mysql_query twice. Take a look at this, which also demonstrates error handling:

[code=php]...

$result = @mysql_query($sql, $conn);
if ($result) {
echo 'Query successful.';
} else {
echo 'Error: ' . mysql_error();
}
?>[/code]


Adam

P.S. You probably want to perform some validation on the posted fields before entering them into the database.
Copy linkTweet thisAlerts:
@k0r54authorAug 13.2004 — Parse error: parse error, unexpected T_IF on line 7

Line seven is if($result) :s any ideas

[code=php]
<?php
$conn = mysql_connect("localhost", "k0m0d0", "ragnarok");
mysql_select_db("ebay",$conn);
$sql = "insert into nametable values('$_POST[name1]', '$_POST[name2],', '$_POST[name3]', '$_POST[name4]')";
$result = mysql_query($sql, $conn)
if ($result) {
echo "error: ". mysql_error();
} else {
echo "sup G";
};
?>
[/code]
Copy linkTweet thisAlerts:
@AdamGundryAug 13.2004 — The previous line ($result = ...) is missing a semicolon.

Edit: you also appear to have reversed the correct and error conditions in the if statement.

Adam
×

Success!

Help @k0r54 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.5,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...