MySQL Update Database Where Post Values is in Array - php

I have a site where users can input multiple values in the text fields, so I created an array for the post values and have successfully inserted the values of the array into the database.
I have a problem updating these values that I have stored in the database. I tried this but it's not working.
<?php
if (!empty($_POST['sub_cat'])) {
$cat_id = $_GET['c_id'];
$subcat_title_array = $_POST['sub_cat'];
for ($i = 0; $i < count($subcat_title_array); $i++) {
$subcat_title = mysqli_real_escape_string($connection, $subcat_title_array[$i]);
$query = "UPDATE sub_categories SET subcat_title = '{$subcat_title}' WHERE subcat_id = $cat_id";
$update_sub_category = mysqli_query($connection, $query);
if (!$update_sub_category) {
die("Query Failed " . mysqli_error($connection));
}
}
}
?>
Is there anything am doing wrong?
Any help will be appreciated.

Related

Facing problem in insert value into multiple row

I am writing a PHP script where it takes data from a database table where there are more than ten rows. After taking all the rows' input from the database it adds with a variable. After that those sum value is inserted to all the rows of another database. My code is working fine in fetching all the rows' data from the database and add a number to that value. But it does not insert new data into another database. I am not getting any error messages. My code:
<?php
include("dbconnect.php");
$query = "SELECT * FROM down_value";
$down_value_db = $conn->query($query);
/*Time Deference Variable*/
$td1=1;
$date = date("Y-m-d");
while ($row = mysqli_fetch_assoc($down_value_db)) {
/*Value from db*/
$s_data1=$row['TGI_R'];
/* Simulated Data*/
$e_data1=$s_data1+$td1;
//Insert Data into database
$insert = $conn->query("INSERT into down_simulation (TGI_R,date) VALUES ('$e_data1', '$date')");
if($insert){
echo "$e_data1 <br/>Successfully data Recorded <br/>";
}else{
echo "Error";
}
}
?>
you must add your select values to array and then with for loop insert this values to another table.
$query = mysqli_query($conn,"select tgi_r from down_value");
$tgi_r_array = array();
$date_time = date("Y-m-d H:i:s");
while ($row = mysqli_fetch_array($query, MYSQLI_BOTH)){
$tgi_r_array[] = $row['tgi_r']+1;
}
for ($i = 0; $i < count($tgi_r_array); $i++){
$insert = mysqli_query($conn, "insert into down_simulation(tgi_r,date) values ('" . $tgi_r_array[$i] . "', '" . $date_time . "')");
}
print_r($tgi_r_array);

inserting in mysqli is not working

I am trying to get data from one table and inserting it in another table.
But the problem is that it is not inserting.
Here is my code
$query = "SELECT id,pname,medicin FROM logpn WHERE id = '$login_session'";
$result = mysqli_query($dbhandle,$query);
while( $row = mysqli_fetch_array($result,MYSQLI_ASSOC)){
$id = $row['id'];
$pname = $row['pname'];
$med = $row['medicin'];}
if (isset($_POST['button1'])) {
mysqli_query($dbhandle,"INSERT INTO medicin_refill (id, pname, medicin) VALUES ('$id', '$pname', '$med')") or die(mysqli_error($dbhandle));
Can anyone tell me what is wrong??
Maybe try VALUES ($id,[...] instead of VALUES ('$id',[...] because the $id shouldn't be a string.

Prevent Form from inserting data multiple times into database

Trying to insert form data into mysql database, my issues is as the user submits the form the data gets inserted twice into database. I know that I am doing something wrong with my 2nd query $query_file = "INSERT INTO upperbit_files... statment as when I remove the whole if loop if(mysqli_query($dbc, $query_info)){...} the form gets submitted once as expected.
Basically I need to insert data into 2 tables. One is for general product info and the other one is to store photos relating to that product both the table are connected via a global variable $advert_id. I am using 2 separate queries
Table1: advert_sell_category1, is for general product info
Table2: upperbit_files, is to store details of the images uploaded
But for some reason the 1st query relating to general product info is getting inserted twice into database and the irony is both the time the $advert_id is the same. Below is my code and a screenshot of the database for your understanding,
if(isset($_POST['postad'])){
$adtype = $_POST['offering_type'];
$manufacturer = mysqli_real_escape_string($dbc, $_POST['manufaturer']);
$mediafile = mysqli_real_escape_string($dbc,$_POST['mediafile']);
$GLOBALS['advrt_post_id'] = crypto_rand_secure(10, 100000);
$query_info = "INSERT INTO advert_sell_category1(advert_id,manufacturer,image_file)
VALUES('$advrt_post_id','$manufacturer','$mediafile')";
$result = mysqli_query($dbc, $query_info) or die(mysqli_error($dbc));
if(mysqli_query($dbc, $query_info)){
$last_id = mysqli_insert_id($dbc);
$query_link_id = "SELECT advert_id FROM advert_sell_category1 WHERE id = '$last_id' ";
$result_id = mysqli_query($dbc, $query_link_id);
while ($row = mysqli_fetch_assoc($result_id)) {
$link_id = $row['advert_id'];
if(!empty($mediafile)){
$media_file = explode(",", mysqli_real_escape_string($dbc,$_POST['mediafile']));
$media_file = array_filter($media_file);
$media_file_size = explode(",", mysqli_real_escape_string($dbc,$_POST['mediafilesize']));
$media_file_size = array_filter($media_file_size);
$media_file_type = explode(",", mysqli_real_escape_string($dbc,$_POST['mediafiletype']));
$media_file_type = array_filter($media_file_type);
for ($var = 0; $var < sizeof($media_file); $var++){
$query_file = "INSERT INTO upperbit_files(file,size,type,link_id) VALUES ('$media_file[$var]','$media_file_size[$var]','$media_file_type[$var]','$link_id')";
$result_file = mysqli_query($dbc, $query_file) or die(mysqli_error($dbc));
}
}
}
}
/********** Your Code ************/
$result = mysqli_query($dbc, $query_info) or die(mysqli_error($dbc));
if(mysqli_query($dbc, $query_info)){
/**********************/
See here in if statement you are calling mysqli_query() second time so same data is inserted twice. Use following code to solve your problem
/********** Suggested Code ************/
$result = mysqli_query($dbc, $query_info) or die(mysqli_error($dbc));
if(mysqli_affected_rows()>0){
/**********************/

Update one value at multiple ID's in database table

$upload_files=implode(' ',$_GET['upload_files']);
$upload_user=",".$_GET['upload_user'];
echo $upload_files;
$sql = "UPDATE {$db_pr}files SET userID = CONCAT(userID,'".$upload_user."') WHERE id IN ('".$upload_files."')";
IN takes a comma-delimited string I believe.
try:
$upload_files=implode("','",$_GET['upload_files']);
Well, I got the solution. I used for loop to achieve the result.
$upload_files=$_GET['upload_files'];
$upload_user=",".$_GET['upload_user'];
for ($i = 0, $count = count($upload_files); $i <= $count; $i++) {
$sql = "UPDATE {$db_pr}files SET userID = CONCAT(userID,'".$upload_user."') WHERE id = '".$upload_files[$i]."'";
$result = mysqli_query($mysqli,$sql) or die("Error occurred - tried to update file.");
}
echo "<div class='loginMessage loginSuccess'>Assigned Successfully!!!</div>";

Using a for loop to insert into mysql

So I have a form with 4 array inputs. When I hit submit it only submits the first array values. ID is the current session holder to make sure that it creates multiple rows with the same ID. I'm trying to have it loop through and insert multiple products under the same ID. Any ideas?
session_start();
$conn = new mysqli("$server", "$user_name", "$password", "$database");
$productName = $_POST['productName'];
$productPrice = $_POST['productPrice'];
$productDescription = $_POST['productDescription'];
$productImage = $_POST['productImage'];
$id = $_SESSION['id'];
$productNameCount = count($productName);
if (isset($productName) && isset($productPrice) && isset($productDescription) && isset($productImage)) {
for ($i = 0; $i < $productNameCount; $i++) {
$productNumber = $i + 1;
$sql .= "INSERT INTO Products (id, productNumber, productName, productPrice, productDescription, productImage)
VALUES ('$id', '$productNumber', '$productName[$i]', '$productPrice[$i]',
'$productDescription[$i]', '$productImage[$i]');";
}
$conn->query($sql);
}
mysqli_close($conn);
First you have to find a way to get whole array.
I will write a small example:
var jsonString = JSON.stringify(array_in_javascript);
This is a in javascript.
And then in PHP:
$names = json_decode(stripslashes($_POST['jsonString']));
You must find a way related to your code.
also you can benefit from prepare statement to insert multiply records.

Categories