Aysha Azura
Aysha Azura

Reputation: 179

value is not inserting into database and the code is free from error

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())");

}

Upvotes: 0

Views: 51

Answers (5)

Roopendra
Roopendra

Reputation: 7762

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;
}

Upvotes: 1

Vikas Gautam
Vikas Gautam

Reputation: 978

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')");

Upvotes: 1

user2936213
user2936213

Reputation: 1011

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.

Upvotes: 1

Jenz
Jenz

Reputation: 8369

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())");

Upvotes: 1

Jignesh Patel
Jignesh Patel

Reputation: 1022

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."')");

Upvotes: 1

Related Questions