/    Sign up×
Community /Pin to ProfileBookmark

query won’t insert into db

the following code when executed gives back an error stating that the query is empty.

[code=php]?php

include(‘db.php’);

if($_POST[‘btnSubmit’]) {
$accountCompany = $_POST[‘accountCompany’];
$accountWebsite = $_POST[‘accountWebsite’];
$accountGUID = md5(uniqid(1));
if($accountCompany != ” and $accountWebsite != ”) {
$sql = “INSERT INTO tblAccounts (accountCompany, accountWebsite, accountGUID) VALUES (‘” . $accountCompany .”‘,'” . $accountWebsite. “‘, ‘” . $accountGUID . “‘)”;
$result = mysql_query($query) or die(‘Error : ‘ . mysql_error());
//if (mysql_query($sql, $conn)){

header(“Location: accounts.php?new=true”);

//}
} else {
$err = true;
}
}
?>[/code]

i can’t figure it out

to post a comment
PHP

9 Comments(s)

Copy linkTweet thisAlerts:
@chazzyApr 19.2006 — can you copy and paste the entire error here?

[code=php]
$result = mysql_query($query) or die('SQL: '.$query.'n<br />Error : ' . mysql_error());[/code]
Copy linkTweet thisAlerts:
@MegatronauthorApr 19.2006 — "Error : Query was empty" is all i get back
Copy linkTweet thisAlerts:
@chazzyApr 19.2006 — sometimes we all miss the slightest errors:
[code=php]
$sql = "INSERT INTO tblAccounts (accountCompany, accountWebsite, accountGUID) VALUES ('" . $accountCompany ."','" . $accountWebsite. "', '" . $accountGUID . "')";
$result = mysql_query($query) or die('Error : ' . mysql_error());
[/code]

You define $sql as your insert, yet you call mysql_query against $query, which I don't see as defined.
Copy linkTweet thisAlerts:
@MegatronauthorApr 19.2006 — thanks chazzy, i got it working (kind of), it was a problem with the database connection. But could you explain to me what needs to be done to resolve the issue completly, i'm terrible at PHP, i have spent far too long at this and i'm still no closer to getting it sorted.

The script should update the field AccountGuid but it doesn't

[code=php]
<?


MySQL username and password
$conn = mysql_connect("localhost","","");

mysql_select_db("database", $conn);
$id=$_GET['id'];
$accountGUID=$_POST['accountGUID'];
if($_POST['btnSubmit']) {
$accountGUID = md5(uniqid(1));
if($accountCompany != '' and $accountWebsite != '') {
$sql = "UPDATE tblAccounts SET accountGUID = '$accountGUID' WHERE id = '$id'";
if (mysql_query($sql, $conn)){

header("Location: accounts.php?new=true");

}
} else {
$err = true;
}
}
?>
[/code]
Copy linkTweet thisAlerts:
@chazzyApr 19.2006 — I'm not sure what you mean by "it was a problem with the database connection" because based on the code you provided, it isn't. it's a problem with not sending in the right variable.

Let's start by doing some error checking

[code=php]
$result = mysql_query($sql, $conn) or die("Unable to issue update: ".$sql." returned error: ".mysql_error());
$numrows = mysql_affected_rows();
if($numrows > 0){
header("Location: accounts.php?new=true");
}
else{
echo "No rows were updated.";
}[/code]


Also, it's a little weird how you're sending both get and post parameters. is this infact the case?
Copy linkTweet thisAlerts:
@MegatronauthorApr 19.2006 — i'm reading in the id from another page so i'm using Get to bring the variable id over and then posting the accountGUID variable into the database
Copy linkTweet thisAlerts:
@chazzyApr 19.2006 — ok, but what happens w/ the error checking? is it failing out, is it giving no rows?

can you verify that the two vars are defined?
Copy linkTweet thisAlerts:
@MegatronauthorApr 19.2006 — ok, but what happens w/ the error checking? is it failing out, is it giving no rows?

the page renders, but doesn't update..no error. This is the code i'm using, i hope i have it setup correctly
[code=php]<?php

//connect to the database - change USERNAME and PASSWORD to your MySQL username and password
$conn = mysql_connect("localhost","chefshop_test","cormac");
//choose the tracking db
mysql_select_db("chefshop_test", $conn);

$id=$_GET['id'];
$accountGUID=$_POST['accountGUID'];

if($_POST['btnSubmit']) {
$accountGUID = md5(uniqid(1));
if($accountCompany != '' and $accountWebsite != '') {
$sql = "UPDATE tblAccounts SET accountGUID = '$accountGUID' WHERE id = '$id'";
if (mysql_query($sql, $conn)){
$result = mysql_query($sql, $conn) or die("Unable to issue update: ".$sql." returned error: ".mysql_error());
$numrows = mysql_affected_rows();
if($numrows > 0){
header("Location: accounts.php?new=true");
}
else{
echo "No rows were updated.";
}

}
}
}
?>[/code]


can you verify that the two vars are defined?[/QUOTE]

wouldn't

$id=$_GET['id'];

$accountGUID=$_
POST['accountGUID'];

define the variables?
Copy linkTweet thisAlerts:
@chazzyApr 19.2006 — yes, that would define them, but they would have no value if $_GET['id'] or $_POST['accountGUID'] are not set.

as for your code:
[code=php]
if (mysql_query($sql, $conn)){
$result = mysql_query($sql, $conn) or die("Unable to issue update: ".$sql." returned error: ".mysql_error());
[/code]

I should have made it clearer when I first posted, but only issue your query once.

[code=php]

//connect to the database - change USERNAME and PASSWORD to your MySQL username and password
$conn = mysql_connect("localhost","chefshop_test","cormac");
//choose the tracking db
mysql_select_db("chefshop_test", $conn);

$id=$_GET['id'];
$accountGUID=$_POST['accountGUID'];
if($_POST['btnSubmit']){
$accountGUID = md5(uniqid(1));
if($accountCompany != '' and $accountWebsite != '') {
$sql = "UPDATE tblAccounts SET accountGUID = '$accountGUID' WHERE id = '$id'";
$result = mysql_query($sql, $conn) or die("Unable to issue update: ".$sql." returned error: ".mysql_error());
$numrows = mysql_affected_rows();
if($numrows > 0){
header("Location: accounts.php?new=true");
}
else{
echo "No rows were updated.";
}
}
}
[/code]


Use that block of code as a replacement for what you have above.
×

Success!

Help @Megatron 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.17,
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,
)...