0

I want to check if a user is already there on my 'sellers' table by checking if his email already exists on the table. If it exists, i want to skip the insertion of a new record, else insert the new seller. the code is as follows:

$email = $_REQUEST['semail'];
$sql = 'SELECT Email FROM project2.sellers WHERE Email = :email';
$s = $pdo->prepare($sql);
$s->bindValue(':email', $email);
$s->execute();
if (!$s) {

}
if (mysql_num_rows($s)!=0) {
  echo "exists";
}
else
{
$sql = 'INSERT INTO project2.sellers SET
        Type = :stype;
        Name = :sname;
        Email = :semail;
        Phone = :sphone;
        Location = :location;
        City = :city';
$s = $pdo->prepare($sql);
$s->bindValue(':stype', $_REQUEST['stype']);
$s->bindValue(':sname', $_REQUEST['sname']);
$s->bindValue(':semail', $_REQUEST['semail']);
$s->bindValue(':sphone', $_REQUEST['sphone']);
$s->bindValue(':location', $_REQUEST['location']);
$s->bindValue(':city', $_REQUEST['city']);
$s->execute();
}
header('location: ../'.$_REQUEST['category']);

please tell me where am i going wrong

Chirag Agarwal
  • 23
  • 1
  • 1
  • 4
  • 1
    `INSERT TO .. SET` really? I think that's not standard but possible using procedures – Mark Oct 23 '13 at 07:26

1 Answers1

2

You should be using $s->rowCount(); not mysql_num_rows();

Read more about it here: http://php.net/manual/en/pdostatement.rowcount.php

And as Christian commented above, your MySQL syntax looks funky.
Should look something more like this:

INSERT INTO table_name (column1, column2, column3,...)
VALUES (value1, value2, value3,...)
Ben Poulson
  • 3,368
  • 2
  • 17
  • 26
  • $s = $pdo->prepare("INSERT INTO products (adDate, Category, Subcat, Title, Description, Photo, Price, sellerEmail) VALUES (CURDATE(), :category, :subcat, :title, :description, :file, :price, :semail"); $s->bindParam(':category', $_REQUEST['category']); $s->bindParam(':subcat', $_REQUEST['subcat']); $s->bindParam(':title', $_REQUEST['title']); $s->bindParam(':description', $_REQUEST['desc']); $s->bindParam(':file', $_FILES["file"]["name"]); $s->bindParam(':price', $_REQUEST['price']); $s->bindParam(':semail', $_REQUEST['semail']); $s->execute(); – Chirag Agarwal Oct 23 '13 at 08:39
  • When I follow this syntax as suggested by you, its giving me an error at the last line i.e. $s->execute();It says syntax error or access violation : 1064 – Chirag Agarwal Oct 23 '13 at 08:48
  • You're missing the closing bracket on the VALUES() part of the statement. – Ben Poulson Oct 23 '13 at 12:45
  • Brilliant! Feel free to mark this answer as the correct one. :) – Ben Poulson Oct 23 '13 at 15:29