Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I am trying to insert multiple records in a for loop like so:
$connection = mysqli_connect("localhost", "username", "password");
mysqli_select_db($connection, "database");
for ($i = 1; $i <= $_POST['people']; $i++) {
$stmt = "";
if ($stmt = $connection->prepare("INSERT INTO `table` (firstname, lastname, email, rsvp) VALUES (?,?,?,?)")) {
$stmt->bind_param('ssss', "James", "Smith", "smith#abc.com", "yes");
$stmt->execute();
$stmt->close();
}
}
mysql_close($connection);
But its not inserting, I put in an echo at the beginning of the loop and it only echos once. Please help.
Try assigning all of your customer data to variables first, like so:
$firstname = 'James';
$lastname = 'Smith';
$email = 'smith#abc.com';
$rsvp = 'yes';
$stmt->bind_param('ssss', $firstname, $lastname, $email, $rsvp);
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I'm learning PDO. Why is the below not working? Where is my mistake / error?
I just want the query to retrieve the highest number from seconds column
..................................................
if ($_SERVER['HTTP_X_FORWARD_FOR']) {
$ipaddress = $_SERVER['HTTP_X_FORWARD_FOR'];
} else {
$ipaddress = $_SERVER['REMOTE_ADDR'];
}
$user = "open";
$password = "r23sSF32";
$database_name = "open2";
$hostname = "localhost";
$dsn = 'mysql:dbname=' . $database_name . ';host=' . $hostname;
$conn = new PDO($dsn, $user, $password);
$sql = "SELECT MAX( seconds ) AS seconds FROM `opentill` WHERE ipaddress='$ipaddress'";
$conn->query($sql) as $row
$largests = $row['seconds'];
Try using prepared statements instead. Here's an example (untested):
$stmt = $conn->prepare("SELECT MAX( seconds ) AS seconds FROM `opentill` WHERE ipaddress = :ipaddress");
$stmt->bindParam(":ipaddress", $ipaddress); // Note: bindParam binds to the REFERENCE of the variable passed, only evaluated when execute() is called
$stmt->execute();
$result = $stmt->fetchColumn();
$result now contains that column value.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
the below php code doesn't return anything while the below one returns value, the only difference is the multiple selection. why is that?
(When I test is in my browser it dosen't shot anything)
I also tried to put the selection between () but doesn't help.
NON Working code:
<?php
$mysqli = new mysqli("x", "w", "y", "z");
$coresite = "Abbasya";
$rowx = "103";
$columnx = "3";
$directionx = "Back";
if($stmt = $mysqli->prepare("SELECT CABOwner, EtisaatTeam FROM CAB WHERE (SiteName=? AND Row=? AND Col=? AND Direction=?)"))
{
$stmt->bind_param("ssss", $coresite, $rowx, $columnx, $directionx);
$stmt->execute();
$stmt->bind_result($cabinet);
while ($stmt->fetch())
{
echo json_encode($cabinet).",";
}
$stmt->close();
}
else{
$mysqli->close();
}
?>
Working Code with one selection:
<?php
$mysqli = new mysqli("x", "w", "y", "z");
$coresite = "Abbasya";
$rowx = "103";
if($stmt = $mysqli->prepare("SELECT DISTINCT Col FROM CAB WHERE (SiteName=? AND Row=?)"))
{
$stmt->bind_param("ss", $coresite, $rowx);
$stmt->execute();
$stmt->bind_result($Col);
while ($stmt->fetch())
{
echo json_encode($Col).",";
}
$stmt->close();
}
else{
$mysqli->close();
}
?>
Since it returns multiple values you need to bind to multiple variables. Like $stmt->bind_result($Col1, $Col2);
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
hello i have this code which try to insert data in the database but i face error rong parameter count for mysqli
<?php
session_start();
$regValue = $_GET['regName'];
echo "Your registration is: ".$regValue.".";
$servername = "localhost";
$username = "root";
$password = "b4sonic";
$dbname = "b4sonic2";
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// prepare and bind
$stmt = $conn->prepare("INSERT INTO b4sonic (first_name) VALUES (?)");
$stmt->bind_param( $firstname);
// set parameters and execute
$firstname = "John";
$stmt->execute();
echo "New records created successfully";
$stmt->close();
$conn->close();
?>
please i ness rapid respone because i work on project and i should comlete it but this probelm
Fred addressed one issue. At the time of bind_param, $firstname isn't defined.
You also aren't calling bind_param correctly.
The first parameter needs to identify the variable type. For instance 's' for string. Refer to the link I added.
// define the variable first
$firstname = "John";
// prepare and bind
$stmt = $conn->prepare("INSERT INTO b4sonic (first_name) VALUES (?)");
$stmt->bind_param("s", $firstname);
// set parameters and execute
$stmt->execute();
put $firstname above your line:
// prepare and bind
$firstname = "John";
$stmt = $conn->prepare("INSERT INTO b4sonic (first_name) VALUES ('$firstname')");
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I want to insert the values of a form into the database table called company. But this program again and again tells me that my query did not get executed. May I know why this is so??
I have stored all the values of the form into an array called insert and than I implode the array and name it as newarray so that it is in a form which can be inserted into the database.
But this does not seem to work for me. Please tell me what's the problem in here??
<?php
$host = "localhost";
$name = "root";
$password = "";
$db = "shopinz";
$con = mysqli_connect($host,$name,$password,$db);
$insert = array();
$newarray = array();
if(mysqli_connect_errno()){
echo("Cannot connect to the databse".mysqli_connect_errno());
exit();
}
else{
if($_SERVER['REQUEST_METHOD'] == 'POST'){
foreach($_POST as $value){
if($value == $_POST['submit']){
break;
}
else{
array_push($GLOBALS['insert'],$value);
}
}
$newarray = implode(',',$insert);
$result = mysqli_query($con,"INSERT INTO company (company_name,company_number,company_address) VALUES($newarray)");
if($result){
echo("1 row added");
}
else{
echo("Query not executed");
}
}
}
?>
This line has no significance:
array_push($GLOBALS['insert'],$value);
Push your values to the one that you need which is $insert
unset($_POST['submit']);
foreach($_POST as $value){
$insert[] = "'".$con->real_escape_string($value)."'";
}
$newarray = implode(',',$insert);
Note: I suggest use prepared statements instead.
$insert = $con->prepare('INSERT INTO company (company_name,company_number,company_address) VALUES(?, ?, ?)');
$insert->bind_param('sss', $_POST['company_name'], $_POST['company_number'], $_POST['company_address']);
$insert->execute();
Looking at your code the problem I can see is here:
foreach($_POST as $value){
In your foreach you have $_POST but you dont define what posit it si it should be $_POST['something']
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
<?php
require('database.php');
$user = $_POST["username"];
$password = $_POST["password"];
$location = $_POST["location"];
$stmt = $mysqli->prepare("insert into Userinfo (username, password, location) values (?, ?, ?)");
if(!$stmt) {
//printf("Query prep failed: %s\n", mysqli->error);
echo "query prep failed".$mysqli->error;
exit;
}
$stmt->bind_param('sss', $username, $password, $location);
$stmt->execute();
$stmt->close();
error_log("username ".$user, 3, "/tmp/php_error.log");
}
?>
Database.php
<?php
$mysqli = new mysqli('localhost', 'php', 'passtheword', 'Android');
if($mysqli->connect_errno) {
printf("Connection Failed: %s\n", $mysqli->connect_error);
exit;
}
?>
This query is not modifying my database for some reason. I know 'database.php' is valid, and I don't get an error from the if(!$stmt) section. Nothing breaks, it just doesn't modify the table, Userinfo. Can anyone tell me why?
Change $user to $username. You are binding and inserting $username but you only $_POST to and define $user