PHP & MySQL EDITING & UPDATING PROBLEM

Daer All

I am using below script to update my entry from mysql table, but i am getting the following error message.

"
Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\xampp\mehidyxampp\form\update.php on line 24"



<?php 
 
// PUT YOUR DATABASE CONNECTION SCRIPT HERE 
$host="localhost"; // Host name
$username="root"; // Mysql username
$password=""; // Mysql password
$db_name="merchandising"; // Database name
 
// Connect to server and select databse. 
 
mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
 
mysql_select_db("$db_name")or die("cannot select DB"); 
 
echo ""; 
?>

<?php

 if(!isset($_POST['submit'])){
 
 $q = "SELECT * FROM newcomp WHERE ID = $_GET[id]";
 $result = mysql_query($q);
 $f = mysql_fetch_array($result);
 $factory = $f['factory'];
 $fdepartment = $f['fdepartment'];
 $designation = $f['designation'];
 $mrchand_name = $f['mrchand_name'];
 $secret_code = $f['secret_code'];
 $uniqkey = $f['uniqkey'];
 $buyinghouse = $f['buyinghouse'];
 $buyer = $f['buyer'];
 $departmentname_no = $f['departmentname_no'];
 
 }

?>


Kindly help me.

This is because there was an error in your query, in which case $result is set to false. You should check for errors arter each mysql_query like you do with mysql_connect and mysql_select_db.

try:


$id =  $_GET['id'];

$q = "SELECT * FROM newcomp WHERE ID =$id";
 $result = mysql_query($q) or die(mysql_error() . $q);
 $f = mysql_fetch_assoc($result);
 $factory = $f['factory'];
 $fdepartment = $f['fdepartment'];
 $designation = $f['designation'];
 $mrchand_name = $f['mrchand_name'];
 $secret_code = $f['secret_code'];
 $uniqkey = $f['uniqkey'];
 $buyinghouse = $f['buyinghouse'];
 $buyer = $f['buyer'];
 $departmentname_no = $f['departmentname_no'];

Please show some example. Could you please correct my script?

I did :wink:

I have solved my first problem. Now I am facing problem to update my tabel with below script.

"
Mysql error, data not saved, try again -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 ‘WHERE id=’‘’ at line 5
"


<?php
$host="localhost"; // Host name
$username="root"; // Mysql username
$password=""; // Mysql password
$db_name="merchandising"; // Database name
//$tbl_name="test_mysql"; // Table name

// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");

// update data in mysql database
//$sql="UPDATE test_mysql SET name='$name', lastname='$lastname',  
//$result=mysql_query($sql);

           $sql = mysql_query("UPDATE test_mysql 
                                       SET   name='$name',
                                             lastname='$lastname',
                                             email='$email',
                                             WHERE id='$id'"); 



                                             if (!$sql) {  
           echo 'Mysql error, data not saved, try again -' . mysql_error();               
             }else{ 
             // echo 'Your data was inserted'; 
             echo "<a href='list_records.php'>View result</a>";
  }


?>

Please help.


//Your code;
           $sql = mysql_query("UPDATE test_mysql  
                                       SET   name='$name', 
                                             lastname='$lastname', 
                                             email='$email', 
                                             WHERE id='$id'"); 
 
//After modifications;
//(means after last column no ',' required and $id is not defined above the code;
$id=$_GET['id'];
$sql = mysql_query("UPDATE test_mysql  
                                       SET   name='$name', 
                                             lastname='$lastname', 
                                             email='$email'
                                             WHERE id='$id'");