0

This is what I have done in my coding part. The code is free from errors but the values are not inserting

$name = $_POST['name'];
$cname = $_POST['cname'];
$caddr = $_POST['caddr'];
$password = $_POST['password'];
$cpassword = $_POST['cpassword'];
$target_path = "upload/";
$target_path = $target_path.basename($_FILES['photo']['name']); 

move_uploaded_file($_FILES['photo']['tmp_name'], $target_path);
$str = "WEB";
$cnt = "SELECT COUNT(no) AS count FROM user";
$result = mysql_query($cnt);
$row = mysql_fetch_object($result);
$res = $row->count;
$res = $res+1;
$uid = $str.$res;
if($password==$cpassword) {
mysql_query("insert into user (no, uid, name, password, cpassword, photo) values('$res', '$uid', '$name', '$password', '$cpassword', '$target_path', now())");

}
Aysha Azura
  • 179
  • 1
  • 2
  • 15
  • That code is far from "free from error"... The very first step to do is to actually *check* for any errors. http://php.net/mysql_error – Pekka Feb 05 '14 at 05:09
  • 1
    Then, mandatory reading http://www.php.net/manual/en/security.database.sql-injection.php and [The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead](http://stackoverflow.com/q/13944956) – Pekka Feb 05 '14 at 05:09
  • 1
    you insert query is wrong. Please check it. you wrote 6 column name and value is 7. hows possible? – Maulik patel Feb 05 '14 at 05:09

5 Answers5

1

please check you insert query , you have mentioned only 6 fields in insert statement but provided 7 values in insert statement. Updated query is below.

mysql_query("insert into user (no,uid,name,password,cpassword,photo) values('".$res."','".$uid."','".$name."','".$password."','".$cpassword."','".$target_path."')");
Jignesh Patel
  • 1,028
  • 6
  • 10
1

Error in your insert query. column mismatch while in insertion.

mysql_query("insert into user (no,uid,name,password,cpassword,photo) values('$res','$uid','$name','$password','$cpassword','$target_path',now())");

These are 6 no,uid,name,password,cpassword,photo column you are assigning to insert value but in second part you are inserting value in 7 column

As already mentions in your question comment regarding mysql_* has been deprecated. and here are your query part to catch mysql_error

$sql = "insert into user (no,uid,name,password,cpassword,photo) values('$res','$uid','$name','$password','$cpassword','$target_path',now())";

$result = mysql_query($sql);
if (!$result) {
    echo 'Could not run query: ' . mysql_error();
    exit;
}
Roopendra
  • 7,674
  • 16
  • 65
  • 92
  • 1
    It would be infinitely more productive to teach the OP how to check for such errors themselves, and what else is wrong with their approach – Pekka Feb 05 '14 at 05:11
  • 1
    I agree with you. OP should be take care such basic thing. I will update answer with debugging. – Roopendra Feb 05 '14 at 05:13
1

You have given an extra entry in values in query. Field name for time is missing.

mysql_query("insert into user (no,uid,name,password,cpassword,photo) values('$res','$uid','$name','$password','$cpassword','$target_path',now())");
Jenz
  • 8,280
  • 7
  • 44
  • 77
1

Your insert query is wrong. You have given six column names but values for them respectively are seven.

In your insert query the last value now() this is extra. You have not specified in which column it will get inserted. Specify that.

user2936213
  • 1,021
  • 1
  • 8
  • 19
1

Try it it will work if any issue check your data base field type an d pass the value accordingly

  mysql_query("insert into user (no, uid, name, password, cpassword, photo) values('$res', '$uid', '$name', '$password', '$cpassword', '$target_path')");
Vikas Gautam
  • 997
  • 7
  • 23