Jump to content

Recommended Posts

I am updating my table in the database, I have written the code, and keep getting the following error:

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in C:\Apache2\Apache2\htdocs\SCS\edit_customers.php on line 166

i have commented the line number 166 in bold.

can some one pls let me know if i am doing something wrong?

(the primary key is model_number and the type is varchar)
but i dont think this shud matter??

Script edit_model.php

<? php
require_once("config.php);
?>

<?

function escape_data($data){
if(ini_get('magic_quotes_gpc')){
$data = stripslashes($data);
}

if (function_exists('mysql_real_escape_string')){
global $connection;
$data = mysql_real_escape_string(trim($data), $connection);
} else {
$data = mysql_escape_string(trim($data));
}

return $data;

}

if ((isset($_GET['model_number']))){
($id = $_GET['model_number']);
}
elseif ((isset($_POST['model_number']))){
($id = $_POST['model_number']);
}
else {
echo '<h1 model_number="mainhead">PAGE ERROR</h1>
<p class="error">This page has been accessed in errorpuj.</p><p><br/><br/></p>';
exit();
}

if (isset($_POST['submitted'])){

$errors = array();


//check for first name
if(empty($_POST['category'])){
$error[] = 'You forgot to enter the category.';
} else {
$fn = escape_data($_POST['category']);
}


if(empty($_POST['quantity'])){
$error[] = 'You forgot to enter the quantity.';
} else {
$e = escape_data($_POST['quantity']);
}


if(empty($errors)) {


$query = "SELECT model_number FROM models WHERE quantity= '$e' AND model_number != $id";
$result = mysql_query($query);
if(mysql_num_rows($result)==0){

$query = "UPDATE models SET model_number='$id', category='$fn', quantity='$e' WHERE model_number=$id";

$result = @mysql_query ($query);
if (mysql_affected_rows() == 1) {

echo '<p> The machine detail has been edited.<p>';

} else {
echo '<h1 model_number="mainhead"> System Error</h1>
<p class = "error" > The machine detail could not be edited due to a system error.</p>';
echo '<p>' . mysql_error() . '<br/><br/> Query: ' . $query . '<p>';

exit();
}

}
else {
echo '<h1 model_number="mainhead">Error!</h1>
<p class="error"> The quantity had already been registered.</p>';
}

} else {
echo '<h1 model_number="mainhead">Error!</h1>
<p class="error"> The following error(s) occured:<br/>';
foreach ($errors as $msg){
echo " - $msg<br/>\n";
}

echo '</p><p>Please Try Again. </p><p><br/></p>';

}

}





$query = "SELECT category, model_number, quantity FROM models WHERE model_number=$id";
$result = @mysql_query ($query);

if (mysql_num_rows($result) ==1){ [/u] [b] //this is line no 166[/b]

$row = mysql_fetch_array ($result, MYSQL_NUM);


echo '<h2> Edit Customer </h2>
<form name="edit_cust_details.php" action = "edit_model.php" method = "post">
<p>Category: <input type="text" name ="category" size="15" maxlength="15" value="' .$row[0] . '"/></p>
<p>Model_number: <input type="text" name ="model_number" size="15" maxlength="15" value="' .$row[1] . '"/></p>
<p>Quantity: <input type="text" name ="quantity" size="15" maxlength="15" value="' .$row[2] . '"/></p>
<p><input type="submit" name="submit" value="Submit" /></p>
<input type="hidden" name="submitted" value="TRUE" />
<input type="hidden" name="model_number" value="' . $id . '" />
</form>';


} else {
echo '<h1 model_number="mainhead">Page Error</h1>
<p class="error">This page has been accessed in error.</p><p><br/><br/></p>';
}

mysql_close();

?>



Link to comment
https://forums.phpfreaks.com/topic/9243-help-with-edit-query/
Share on other sites

You have an error in your sql query that is why your are retrieving that error. So change the following:
[code] $query = "SELECT category, model_number, quantity FROM models WHERE model_number=$id";
$result = @mysql_query ($query);

if (mysql_num_rows($result) ==1){[/code]to[code] $query = "SELECT category, model_number, quantity FROM models WHERE model_number=$id";
$result = @mysql_query ($query) or die("Query error: <i>" . $query . "</i><br><br>" . mysql_error());

if (mysql_num_rows($result) ==1){[/code]

When you run your edit script again PHP/MySQL will report a proper error message in the following format:
[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]Query error: [i][query here][/i]

[mysql error here][/quote]
Link to comment
https://forums.phpfreaks.com/topic/9243-help-with-edit-query/#findComment-34051
Share on other sites

hi. thanks for ur help. i made the change to my sql statement, and now i am getting the following error:

Query error: SELECT category, model_number, quantity FROM models WHERE model_number=\'. $row[\'model_number\']. \'

You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near '\'. $row[\'model_number\']. \'' at line 1

is there anything else wrong with my code?
like i sed i am using the primary key to be model_number which has a type varchar, as it contains both numbers and letters in it.
could that be the problem.
Link to comment
https://forums.phpfreaks.com/topic/9243-help-with-edit-query/#findComment-34067
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.