/    Sign up×
Community /Pin to ProfileBookmark

What’s wrong with my MySQL Statement?

I’m trying to add some info to my database via a form. I’ve done this int he past with no issues, but for some reason I’m having a major brain fart and it stinks.

Here’s my PHP:

[code=php]
//Connect to Database
$con = mysql_connect(“localhost:8888″,”root”,”root”);
if (!$con)
{
die(‘Could not connect: ‘ . mysql_error());
}
mysql_select_db(“toronto_****burns”, $con);

//Add Variables Condition
if ($_GET[‘formUpload’] == 1) {
//Gather variables from Form
$firstName = $_POST[‘first_name’];
$secondName = $_POST[‘second_name’];
$lastName = $_POST[‘last_name’];
$birthDate = $_POST[‘birth’];
$deathDate = $_POST[‘death’];
$genNum = “”;
$picURL = “”;
$email = “”;

//Add variables to Database
$addMember = mysql_query(“INSERT INTO family (first_name, second_name, last_name, birth_date, death_date)
VALUES (‘$firstName’, ‘$secondName’, ‘$lastName’, ‘$birthDate’, ‘$deathDate'”);
if (!mysql_query($addMember,$con)) {
die(‘Error: ‘ . mysql_error());
}
echo “1 record added”;

//echo “” . $firstName . ” ” . $secondName . ” ” . $lastName . ” was born in ” . $birthDate . ” and died in ” . $deathDate;
}
[/code]

For some reason I keep getting “Error: Query was empty”. I’ve tried using $_POST[‘first_name’], etc instead of straight variables and nothing changes.

Any ideas? Need to see more? let me know.

Thanks!

to post a comment
PHP

8 Comments(s)

Copy linkTweet thisAlerts:
@NogDogOct 01.2010 — Looks like you want to use mysql_affected_rows() instead of the 2nd instance of mysql_query(). ?
Copy linkTweet thisAlerts:
@brianisbsauthorOct 01.2010 — Like this?

[code=php]
$addMember = mysql_query("INSERT INTO family (first_name, second_name, last_name, birth_date, death_date)
VALUES ('$firstName', '$secondName', '$lastName', '$birthDate', '$deathDate'");
if (!mysql_affected_rows($addMember,$con)) {
die('Error: ' . mysql_error());
}
echo "1 record added";

//echo "" . $firstName . " " . $secondName . " " . $lastName . " was born in " . $birthDate . " and died in " . $deathDate;
}
[/code]


or

[code=php]

if (mysql_affected_rows() == 0) {
....
}

[/code]


Thanks!
Copy linkTweet thisAlerts:
@NogDogOct 01.2010 — Either way. Or you could assign the results to a variable, in case you want to see if for some unexplained reason it inserts more than one row. ? However, note that if the result is 0, mysql_error() won't say anything, as that is not a DB error. If there were an error, it would show up by the mysql_query() failing.
[code=php]
$sql = "
INSERT INTO family (
first_name,
second_name,
last_name,
birth_date,
death_date)
VALUES (
'$firstName',
'$secondName',
'$lastName',
'$birthDate',
'$deathDate'
";
$addMember = mysql_query($sql, $con) or die(mysql_error()."<br />n$sql");
if (($rows = mysql_affected_rows($addMember, $con)) == 0) {
die('Error: Unknown DB error, no row added');
}
else {
echo "$rows record added";
}
[/code]
Copy linkTweet thisAlerts:
@brianisbsauthorOct 01.2010 — Crud... I have a DB error. Looks like I need to review my DB connection. I know the connectiong [b]to[/b] my DB is ok, becasue I can display the information to my page. But for some reason I can't add info to my DB..

Any ideas?
Copy linkTweet thisAlerts:
@brianisbsauthorOct 01.2010 — Correction:

The information uploaded to my DB, but it still gave me an error code. I'm assuming it's something to do with how many rows are being affected, I'll have to test it out..

Thanks!

(sorry for the double-post..)
Copy linkTweet thisAlerts:
@DasherOct 02.2010 — [code=php]$query ="INSERT INTO family (first_name, second_name, last_name, birth_date, death_date)
VALUES ('$firstName', '$secondName', '$lastName', '$birthDate', '$deathDate')";

// you are missing the closing paren of VALUES.

echo $query."<br>";

// try echoing the query to make sure it is filled out like you think it should be.


[/code]
Copy linkTweet thisAlerts:
@brianisbsauthorOct 05.2010 — Hi Dasher,

I'm sorry, I should have added that I...added the parenthesis at the and of the query. Even then, the info is added to my DB, but the error message is still relayed. I'm thinking I'll get rid of the error message and replace it with a congrats message, Lol.
Copy linkTweet thisAlerts:
@SyCoOct 05.2010 — Don't forget to sanitize the user input.
×

Success!

Help @brianisbs 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.20,
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: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

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