is it possible to utilize two $stmt mysqli - php

Hello I have a question when I use my $stmt to execute an insert query into my database it works perfectly fine, however when I use a $stmt2 after that execute to UPDATE a different table it won't update the table even though to my understanding the code is correct.
The code I have tried to fix many times is as so
$mysqli= my database connection
$stmt = $mysqli->prepare("INSERT INTO `test_table`(datenow,test1,test2,test3,test4,test5,test6,test7,test8,
test9,test10,test11,test12,test14,test15,test16,test17,test18,)
VALUES (CURRENT_TIMESTAMP, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)")
$stmt->bind_param('ssssssssssssssssss',$test1,$test2,$test3,$test4,$test5,$test6,$test7,$test8,$test9,$test10,$test11,$test12,$test13,$test14, $test15,$test16,$test17,$test18);
$stmt1 = $mysqli->prepare("UPDATE `users` SET productID='1', purchase_date=CURRENT_TIMESTAMP, end_date=DATE_ADD(CURRENT_TIMESTAMP(), INTERVAL 30 DAY) WHERE username = ?");
$stmt1->bind_param('s', $username);
$stmt1->execute();
$stmt->execute();
Any help would be very appreciated thanks!

A prepared statement can only execute one MySQL query. You can prepare as many statements as you want in different variables
so you can change it as:
$stmt1 = $link->prepare("UPDATE `users` SET productID='1', purchase_date=CURRENT_TIMESTAMP, end_date=DATE_ADD(CURRENT_TIMESTAMP(), INTERVAL 30 DAY) WHERE username = ?");
$stmt1->bind_param('s', $username);
to
$stmt1 = $mysqli->prepare("UPDATE `users` SET productID='1', purchase_date=CURRENT_TIMESTAMP, end_date=DATE_ADD(CURRENT_TIMESTAMP(), INTERVAL 30 DAY) WHERE username = ?");
$stmt1->bind_param('s', $username);
see no need of $link, you can prepare many statement for different variable...Thanks

Related

Data Not inserted in Database php, mysql

Can't insert data into DB . When i remove user_id then data is inserted. Please check below my code and help me.
function adddata($data) {
global $db;
if (is_array($data)) {
$stmt = $db->prepare('INSERT INTO `pay` (id, payment, status, itemid, time, user_id) VALUES(?, ?, ?, ?, ?, ?');
$userid = 2;
$stmt->bind_param(
'sdssss',
$data['txn_id'],
$data['payment_amount'],
$data['payment_status'],
$data['item_number'],
date('Y-m-d H:i:s'),
$userid
);
$stmt->execute();
$stmt->close();
return $db->insert_id;
}
return false;
}
It's subtle, but your SQL string is missing a closing bracket:
$stmt = $db->prepare('INSERT INTO `pay` (...) VALUES (?, ?, ?, ?, ?, ?)');
Where the VALUES list was not properly closed.
A lot of problems can be detected and resolved by enabling exceptions in mysqli so mistakes aren't easily ignored. This should show up as a SQL error in your logs.

php on duplicate key

I'm having trouble trying to get the following to work:
$stmt = $mysqli->prepare("INSERT INTO member_data (member_id, name, address, telephone, mobile) VALUES (?, ?, ?, ?, ?) ON DUPLICATE KEY UPDATE member_data SET name = ?, address = ?, telephone = ?, mobile = ? WHERE member_id = ?");
$stmt->bind_param('ssssssssss', $_SESSION['user_id'], $_POST['new_name'], $_POST['new_address'], $_POST['new_telephone'], $_POST['new_mobile'], $_POST['new_name'], $_POST['new_address'], $_POST['new_telephone'], $_POST['new_mobile'], $_SESSION['user_id']);
$stmt->execute();
$stmt->close();
It doesn't seem to be able to update or insert any values.
I'm sure I've done something wrong, but I've searched and haven't found anything relevant to my issue. Is this the right approach?
EDIT: Working code:
$stmt = $mysqli->prepare("INSERT INTO member_data (member_id, name, address, telephone, mobile) VALUES (?, ?, ?, ?, ?) ON DUPLICATE KEY UPDATE name = ?, address = ?, telephone = ?, mobile = ?");
$stmt->bind_param('sssssssss', $_SESSION['user_id'], $_POST['new_name'], $_POST['new_address'], $_POST['new_telephone'], $_POST['new_mobile'], $_POST['new_name'], $_POST['new_address'], $_POST['new_telephone'], $_POST['new_mobile']);

Prepare Statement Issue sending encrypted information

This is my current statement. Everything was working fine until I added the key
Key is just a generated hash for the user to activate the account.
$stmt = $mysqli->prepare("INSERT INTO Account (accountUsername,accountPassword,accountEmail,accountActivate,accountKey) VALUES (?, ?, ?,?,?)");
$stmt->bind_param('sssiss', $username, $newPassword, $email,0,$key,time());
When I'm doing this code I'm getting an error.
Cannot pass parameter 5 by reference
Do you know what could be the issue?
Thanks!
Edit Code:
$stmt = $mysqli->prepare("INSERT INTO Account (accountUsername,accountPassword,accountEmail,accountActivate,accountKey,accountCreated) VALUES (?, ?, ?,?,?,?)");
$stmt->bind_param('sssisi', $username, $newPassword, $email,0,$key,$time);
http://i.stack.imgur.com/Th5tl.png
If you use bind_param that 0 needs to be in a variable since bind_param passes by reference.
$somevar=0;
$stmt = $mysqli->prepare("INSERT INTO Account (accountUsername,accountPassword,accountEmail,accountActivate,accountKey) VALUES (?, ?, ?, ?,?,?)");
$stmt->bind_param('sssiss', $username, $newPassword, $email,$somevar,$key,$time);

Get last ID after SQL insert

This code below inserts some data into a MySQL database. I want the page to load to this record after the insert but the variable ($insert) that I'm trying to load is not coming through on the header. can someone help?
$pdo = Database::connect();
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "INSERT INTO riders (firstname,secondname,email,mobile,landline,dob,addressline1,town,county,postcode) values(?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
$q = $pdo->prepare($sql);
$q->execute(array($firstname,$secondname,$email,$mobile,$landline,$dob,$streetaddress,$town,$county,$postcode));
$insert = mysql_insert_id();
Database::disconnect();
header("Location:read.php?id=.$insert");
Use $pdo->lastInsertId() for getting last ID after SQL insert.

PHP MySQL INSERT and UPDATE from ODBC

I have a MySQL database that makes a connection to a local MS Access database through ODBC.
I currently have the script properly inserting data from the MS Access database to my MySQL database. The problem is that the MS Access database gets updated daily - so I need code to also update my MySQL database.
Here's what I have - and the result is that i get no errors and nothing is updated (however the insert works fine):
<?php
$conn=odbc_connect('Prod_Schedule','','');
if (!$conn) {
exit("Connection Failed:" . $conn);
}
$sql="SELECT `ID`, `WO_NUM`, `WO_LINE`, `SALES_CCN`, `SO`, `SO_LINE`, `SO_DELIVERY`, `MAS_LOC`, `DUE_DATE`, `FGC`, `HPL`, `DESCRIPTION` FROM `Schedule` WHERE `ID` > $refid AND `HPL` <> 'PART' AND LEN(HPL) > 0";
$rs=odbc_exec($conn,$sql);
if (!$rs) {
exit("Error in SQL");
}
$todays_date = date('m/d/Y', time());
while(odbc_fetch_row($rs)){
$sql = "INSERT INTO `production_schedule` (`ID`, `WO_NUM`, `WO_LINE`, `SALES_CCN`, `SO`, `SO_LINE`, `SO_DELIVERY`, `MAS_LOC`, `DUE_DATE`, `FGC`, `HPL`, `DESCRIPTION`) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
$stmt = $db->prepare($sql);
for($i=1;$i<=odbc_num_fields($rs);$i++){
$stmt ->bindValue($i, odbc_result($rs,$i));
}
$stmt ->execute();
$sqlup = "UPDATE `production_schedule`
SET
`ID` = ?,
`WO_NUM` = ?,
`WO_LINE` = ?,
`SALES_CCN` = ?,
`SO` = ?,
`SO_LINE` = ?,
`SO_DELIVERY` = ?,
`MAS_LOC` = ?,
`DUE_DATE` = ?,
`FGC` = ?,
`HPL` = ?,
`DESCRIPTION` = ?
WHERE `DUE_DATE` < '$todays_date'";
for($i=1;$i<=odbc_num_fields($rs);$i++){
$stmt ->bindValue($i, odbc_result($rs,$i));
}
$stmt ->execute();
}
odbc_close($conn);
?>
You should use PHP cron jobs so you can run daily scripts.
Here is an example.

Categories