简体   繁体   中英

Form not updating database

I have a update form that is populating from a list of employees. The values are passing but not being updated in the database. Here is my code and what I am showing as passed.

<?php

$con = mysql_connect("localhost","root","*******");
 if (!$con)
 {
  die('Could not connect: ' . mysql_error());
  }


$query = mysql_query("select * from backup");

 if(isset($_POST['update']))
 $id = $_POST['id'];
 $first = $_POST['first'];
$last = $_POST['last']; 
$store  = $_POST['store']; 
 $title  = $_POST['title']; 
 $title2  = $_POST['other']; 
 $phone  = $_POST['phone']; 
 $email  = $_POST['email'];
 $dept = $_POST['dept'];
 $bio   = $_POST['bio']; 

 $query="UPDATE backup SET first='$first', last='$last', store='$store',   title='$title', title2='$title2', phone='$phone', email='$email', bio='$bio' WHERE id='$id'";
 mysql_query($query);
 echo "Record Updated";
 mysql_close();
 print_r($_POST)




?>

Here is the result

Record UpdatedArray ( [id] => 1396 [first] => Charles [last] => Adams [store] => [dept] => Accounting [title] => Accounting Clerk [other] => [phone] => 410-555-1212[email] => [email2] => [bio] => Charlie started in August 2009. This is a test.... [Submit] => Submit )

Can someone help me with what I might be doing wrong? As far as injections, I will be fixing that after I finish testing. I know that may sound backwards, but I need to find out why this is not working first.

Thanks for any help with this

You should also select database:

// make foo the current db
$db_selected = mysql_select_db('foo', $link);
if (!$db_selected) {
    die ('Can\'t use foo : ' . mysql_error());
}

But most of all, you should use MySQLi or PDO_MySQL.

This is because, you forgot { after if (isset(...)) .

Also, no database is selected.

Corrected code is as follows:

<?php

$con = mysql_connect("localhost","root","*******");
mysql_select_db('DB_NAME');
 if (!$con)
 {
  die('Could not connect: ' . mysql_error());
  }


$query = mysql_query("select * from backup");

 if(isset($_POST['update'])) {
 $id = $_POST['id'];
 $first = $_POST['first'];
$last = $_POST['last']; 
$store  = $_POST['store']; 
 $title  = $_POST['title']; 
 $title2  = $_POST['other']; 
 $phone  = $_POST['phone']; 
 $email  = $_POST['email'];
 $dept = $_POST['dept'];
 $bio   = $_POST['bio']; 

 $query="UPDATE backup SET first='$first', last='$last', store='$store',   title='$title', title2='$title2', phone='$phone', email='$email', bio='$bio' WHERE id='$id'";
 mysql_query($query);
 echo "Record Updated";
 mysql_close();
 print_r($_POST)
}

?>

You've not defined a database in your connection.

Do this after you've connected to the DB

Your code can be corrected as:

<?php
  $con = mysqli_connect("localhost","root","**","db");
  if (!$con)
  {
   die('Could not connect: ' . mysqli_error());
   }
  //$query = mysqli_query($con,"select * from backup");
  if(isset($_POST['update'])):
  $id = $_POST['id'];
  $first = $_POST['first'];
  //other stuffs
  $query="UPDATE backup SET first='$first', last='$last', store='$store',   title='$title', title2='$title2', phone='$phone', email='$email', bio='$bio' WHERE id='$id'";
  $res = mysqli_query($con,$query);
  if(!$res)
  die("could not update records. Error = ".mysqli_error($con)); 
  echo "Record Updated";
  mysqli_close($con);
  print_r($_POST);
  endif;
  ?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM