php PDO prepare(" INSERT ..(variables ) VALUES(?,?,) produces an error need assistance

StackOverflow https://stackoverflow.com/questions/22229868

  •  06-06-2023
  •  | 
  •  

Domanda

$query = $this->link->prepare("INSERT INTO surveys (`username`,`inspected`,
    `comments`,`ip_address`,`date`,`time`)
        VALUES '(?,?,?,?,?,?)';);
$values = array ($username,$inspected,$comments,$ip_address,$date,$time);
var_dump($query);$rowCount = $query->rowCount();
$return $rowCount;

Nessuna soluzione corretta

Altri suggerimenti

You can base yourself on the following which I've prepared for you.

Sidenote: I'm not entirely sure as to why you want to use rowCount() for, so I left it out for now.

If you're looking to check if a record exists using rowCount(), let me know.

The following method works to insert data into a database, which is based on a method I use.

<?php
$dbname = 'xxx';
$username = 'xxx';
$password = 'xxx';

try {
$pdo = new PDO("mysql:host=localhost;dbname=$dbname", $username, $password);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
 exit( $e->getMessage() );
}

$sql = "INSERT INTO surveys (
        username,
        inspected,
        comments,
        ip_address,
        date,
        time
        ) VALUES (
        :username, 
        :inspected, 
        :comments, 
        :ip_address, 
        :date, 
        :time)";

$stmt = $pdo->prepare($sql);

$stmt->bindParam(':username', $_POST['username'], PDO::PARAM_STR);
$stmt->bindParam(':inspected', $_POST['inspected'], PDO::PARAM_STR);
$stmt->bindParam(':comments', $_POST['comments'], PDO::PARAM_STR);

$stmt->bindParam(':ip_address', $_POST['ip_address'], PDO::PARAM_STR);
$stmt->bindParam(':date', $_POST['date'], PDO::PARAM_STR);
$stmt->bindParam(':time', $_POST['time'], PDO::PARAM_STR);

// $stmt->execute(); 
$stmt->execute(array(':username' => $_POST['username'],':inspected' => $_POST['inspected'],':comments' => $_POST['comments'], 
':ip_address' => $_POST['ip_address'],':date' => $_POST['date'],':time' => $_POST['time']));
if($stmt != false) {
    echo "success!";
} else {
    echo "an error occured saving your data!";
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top