Pass variables from a while to another while - php

I have a little problem with this form.
On the fprm page my code is:
<form action="?act=process" method="post">
<?php $sql = "SELECT * FROM schedule WHERE day = '$day' ORDER BY ID ASC";
$hours = mysql_query($sql);
while ($hour = mysql_fetch_array($hours)) { ?>
<input type="text" name="course" value="<?=$hour[course]?>">
<input type="hidden" name="id" value="<?=$hour[ID]?>">
<?php } ?>
<button type="submit">Submit</button>
On the process page looks like this:
while($id = each($_POST['id']) && $course = each($_POST['course']))
{
//echo "ID $id AND course $course<br/>";
}
All I need to do is to pass all variables in a while to update each of them in my sql database.
The error I get is: Warning: Variable passed to each() is not an array or object in /path_domain
How can I fix this?

The reason is you try to use id/course data as array, but you pass just strings. In order to avoid it, just make you form fields as arrays by adding [] to the fields names:
<input type="text" name="course[]" value="<?=$hour[course]?>">
<input type="hidden" name="id[]" value="<?=$hour[ID]?>">

By adding the [] brackets after the name of the fields.
<form action="?act=process" method="post">
<?php $sql = "SELECT * FROM schedule WHERE day = '$day' ORDER BY ID ASC";
$hours = mysql_query($sql);
while ($hour = mysql_fetch_array($hours)) { ?>
<input type="text" name="course[]" value="<?=$hour[course]?>">
<input type="hidden" name="id[]" value="<?=$hour[ID]?>">
<?php } ?>
<button type="submit">Submit</button>

Related

Insert multiple arrays with PHP PDO

I am doing a program with PHP but this is the first time I need to enter many arrays in a table. In the input I must place [] but when making the query insert how should I do? What I present in the code only saves me the last value without the [], and in the console, it passes the values ​​that I need, but it only inserts the last value.
<form class="" action="asignar-fechas.php" method="post">
<?php foreach ($infoGrupo2 as $iGrupo2){
if(($iGrupo2['fechaInicio']==$fechaA['numero_fecha'])){
?>
<input type="hidden" name="id_grupo2[]" value="<?php echo $iGrupo2['id_grupo'];?>">
<input type="hidden" name="modo2[]" value="<?php echo $iGrupo2['modo'];?>">
<input type="hidden" name="fecha2[]" value="<?php echo $iGrupo2['fechaInicio'];?>">
<input type="hidden" name="participante[]" value="<?php echo $iGrupo['participante'];?>">
<input type="hidden" name="jugador<?php echo $juga;?>[]" value="<?php echo $in2['id_users'];?>">
}}?>
<input type="submit" name="enviar" value="ASIGNAR FECHAS">
asignar-fechas.php
$id_grupo = $_POST['id_grupo2'];
$modo = $_POST['modo2'];
$fecha = $_POST['fecha2'];
$participante = $_POST['participante'];
$j1 = $_POST['jugador1'];
$j2 = $_POST['jugador2'];
$j3 = $_POST['jugador3'];
$j4 = $_POST['jugador4'];
$insertarF = "INSERT INTO fechaxgrupo (grupo, fecha,estado) VALUES (:grupo, :fecha,0)";
$insertF = $conn->prepare($insertarF);
$insertF->bindParam(':grupo', $id_grupo);
$insertF->bindParam(':fecha', $fechaa);
$insertF->execute();
The $_POST variables elements will be arrays, you can loop over them. You need to bind to the iteration variables, not the arrays.
$insertarF = "INSERT INTO fechaxgrupo (grupo, fecha,estado) VALUES (:grupo, :fecha,0)";
$insertF = $conn->prepare($insertarF);
$insertF->bindParam(':grupo', $id_grupo);
$insertF->bindParam(':fecha', $fecha);
foreach ($_POST['id_grupo2'] AS $i => $id_grupo) {
$fecha = $_POST['fecha2'][$i];
$insertF->execute();
}

How to update multiple records in mysql with multiple ids

I have a html form which has 2*12 input fields with name="links[]" and name="ids[]"...
I want to update column 'link' with those 12 links using those 12 ids
I know we need a loop for that.
But don't know how to make the sql query.
$ids=mysqli_real_escape_string($conn, $_POST['ids']);
foreach($ids as $id){....}
$links=mysqli_real_escape_string($conn, $_POST['links']);
foreach($links as $link){....}
$sql="update query.....";
EDIT:
It works with two variables $id and $season but when i add more than two variable like $episode etc. it doesn't work. it doesn't execute other variable only first two are executed and it sets the values of $season to 1 or sometimes 0 of all the entries in the table.
for($i=0 ; $i<count($record['id']); $i++){
$id=mysqli_real_escape_string($conn, $record['id'][$i]);
$season=mysqli_real_escape_string($conn, $record['season'][$i]);
$episode=mysqli_real_escape_string($conn, $record['episode'][$i]);
$rel_id=mysqli_real_escape_string($conn, $record['rel_id'][$i]);
$link=mysqli_real_escape_string($conn, $record['link'][$i]);
//sQl Query
$sql = "UPDATE series SET season='$season' and episode='$episode' and rel_id='$rel_id' and link='$link' WHERE id='$id'";
if ($conn->query($sql) === TRUE) {}
else { echo "Error: " . $sql . "<br>" . $conn->error; };
Try the below code.
<form name="rec" id="rec" method="post">
<input type="text" name="reord[id][]">
<input type="text" name="reord[season][]">
<input type="text" name="reord[episode][]">
<input type="text" name="reord[rel_id][]">
<input type="text" name="reord[link][]">
<br/>
<input type="text" name="reord[id][]">
<input type="text" name="reord[season][]">
<input type="text" name="reord[episode][]">
<input type="text" name="reord[rel_id][]">
<input type="text" name="reord[link][]">
<br/>
<input type="text" name="reord[id][]">
<input type="text" name="reord[season][]">
<input type="text" name="reord[episode][]">
<input type="text" name="reord[rel_id][]">
<input type="text" name="reord[link][]">
</br>
<input type="submit" name="submit" value="submit">
</form>
<?php
if(isset($_POST['submit'])){
$record = $_POST['reord'];
$id = $season = $episode = $rel_id = $link = '';
for($i=0 ; $i<count($record['id']); $i++){
$id= mysqli_real_escape_string($conn, $record['id'][$i]);
$season=mysqli_real_escape_string($conn, $record['season'][$i]);
$episode=mysqli_real_escape_string($conn, $record['episode'][$i]);
$rel_id=mysqli_real_escape_string($conn, $record['rel_id'][$i]);
$link= mysqli_real_escape_string($conn, $record['link'][$i]);
echo $sql = "UPDATE series SET season='$season' and episode='$episode' and rel_id='$rel_id' and link='$link' WHERE id=$id";
$conn->query($sql);
}
}
?>
$ids=mysqli_real_escape_string($conn, $_POST['ids']);
foreach($ids as $key => $id){
$sql="update tablename set fieldname=value where link=$_POST[links][$key] and id=$id";
}
This might work. If not give me more details.
UPDATE `tablename` SET `fieldname` = CASE
WHEN id = 1 AND xyz = 3 THEN `value 1`
WHEN id = 2 AND xyz = 3 THEN `value 2`
WHEN id = 3 AND xyz = 3 THEN `value 3`
END
In the scenario described I would use SET-WHEN-THEN query

PHP & MySqli - How to search and work on 'LIKE' even there is additional characters on the textbox?

how can I search the like value or similar value even there is an additional characters on the textbox? right now i cant get rid of the additional characters because the are fixed.
here is the sample image
so that is my textbox now i want to get the similar value like the 44444444
and here is my table content
and here there is the similar and like values
here is my code
<form method="POST" action="">
<input type="text" name="id" id="card-code" value='<?php echo $code ?>' class="form-control">
<input type="submit" name="search" value="Search Data" class="btn btn-primary"></input>
</form>
<?php
$connection =mysqli_connect("localhost","root","");
$db = mysqli_select_db($connection,'inflightapp');
if (isset($_POST['search'])) {
$id = $_POST['id'];
$query = "SELECT * FROM scratch_cards WHERE code LIKE '$id' ";
$query_run = mysqli_query($connection,$query);
while($row = mysqli_fetch_array($query_run))
{
?>
<form action="" method="POST">
<input type="hidden" name="id" value="<?php echo $row['id'] ?>"></input>
<input type="text" name="amount" value="<?php echo $row['amount'] ?>"></input>
</form>
<?php
}
}
?>
You can play with the php string functions to get the exact string as:
$str = ltrim( strstr("Qr Code: 444444444 | 123",":"),":");
$strFinalString = trim(substr($str,0,strpos($str, "|")));
echo $strFinalString;
Please suggest if this code requires some optimisation.

Checkbox in a while loop php

I have a check box inside a while loop like this:
<form method="POST">
<?php $sql= mysql_query("SELECT * FROM names WHERE `id` ='$id' ");
while ($get = mysql_fetch_array($sql)){ ?>
<input type="checkbox" name="id_names" value="<? echo $get ['id'];?>"><?php echo $get ['name']; ?>
<?php } ?>
<input id="submitbtn" type="submit" value="Submit" /><br><br>
</form>
The problem is at this part I am unable to get specific checkbox properties and even if the user selects two check boxes I am unable to echo the id out
<?php
if(isset($_POST['id_names']))
{
$id_names= $_POST['id_names'];
$email = mysql_query("SELECT `email` FROM users WHERE `id` = '$id_names' ");
while ($getemail = mysql_fetch_array($email))
{
echo $getemail['email'];
}
}
?>
I have tried searching for answers but I am unable to understand them. Is there a simple way to do this?
The form name name="id_names" needs to be an array to allow the parameter to carry more than one value: name="id_names[]".
$_POST['id_names'] will now be an array of all the posted values.
Here your input field is multiple so you have to use name attribute as a array:
FYI: You are using mysql that is deprecated you should use mysqli/pdo.
<form method="POST" action="test.php">
<?php $sql= mysql_query("SELECT * FROM names WHERE `id` =$id ");
while ($get = mysql_fetch_array($sql)){ ?>
<input type="checkbox" name="id_names[]" value="<?php echo $get['id'];?>"><?php echo $get['name']; ?>
<input type="checkbox" name="id_names[]" value="<?php echo $get['id'];?>"><?php echo $get['name']; ?>
<?php } ?>
<input id="submitbtn" type="submit" value="Submit" /><br><br>
</form>
Form action: test.php (If your query is okay.)
<?php
if(isset($_POST['id_names'])){
foreach ($_POST['id_names'] as $id) {
$email = mysql_query("SELECT `email` FROM users WHERE `id` = $id");
$getemail = mysql_fetch_array($email); //Here always data will single so no need while loop
print_r($getemail);
}
}
?>

Submitting always gets the last value

<form action="" method="post">
<?php
include 'Includes/database_connection.php';
$sql = "select * FROM sims" ;
$result = mysql_query($sql,$con);
while($row = mysql_fetch_assoc($result)){
?>
<ul class="category_list">
<input type="hidden" value="$id1" name="hidden">
<li><?php echo $row['phonenr'];?><input type="hidden" value="<?php echo $row['id'];?>" name="id"></li>
</ul>
<?php
}
?>
<input type="submit" name="submit">
</form>
So i got the above form where you can select phonenumbers and when you submit them a database should be updated. And there are 23 id's in it. After submitting the form it always takes the last value. What am i doing wrong?
if(#$_POST ['submit'])
{
$id = $_POST["id"];
echo $id;
include 'Includes/database_connection.php';
mysql_query("UPDATE pairings SET sim_id='$id'
WHERE unit_id='$id1'")
}
Change your hidden field name to array like this
<input type="hidden" value="<?php echo $row['id'];?>" name="id[]">
then on PHP side use loop to retrieve
foreach ($_POST['id'] as $val) {
$id = $val;
include 'Includes/database_connection.php';
mysql_query("UPDATE pairings SET sim_id='$id'
WHERE unit_id='$id1'")
}
Slight modification specified by chandresh_cool, would get the result that you expect.
The input name is replaced with id, so the post key contains only the row[id], not the $_POST['id']
Instead change the name of the hidden field to accept as a array like this
<input type="hidden" value="<?php echo $row['id'];?>" name="id[]">
Then you can iterate id array as specified by chandresh_cool

Categories