problems with my submit - php

I'm using multiple forms to select what to update. only my last form disappears after submitting. I don't think it even processes the update it should do on submit.
please help me!
php code:
if ($collect == "foto"){
$sql = mysqli_query($link, "SELECT * FROM foto");
$row = mysqli_fetch_assoc($sql);
$array = array();
echo "<form method='POST'>";
while ($row = mysqli_fetch_assoc($sql))
{
echo "<input type='text' name='foto[]' value=".htmlspecialchars($row['foto'])."></input>";
}
echo "<input type='submit' name='submit3' value='update'></input></form>";
if (isset($_POST['submit3'])) {
echo "Finally!";
$array = $_POST['foto'];
$i = 0;
foreach ($array as $foto) {
$ufoto = $array[$i];
$sql ="
UPDATE
foto
SET
foto = '$ufoto'
WHERE
id = $i
";
mysqli_query($link, $sql);
$i ++;
var_dump($array);
}
}
}

$_POST['foto'] = $array['foto'];
should most likely be:
$array = $_POST['foto'];
your script is also open to SQL injection attacks.

Before the edit I saw you neglected to put an action-attribute in your form-tag. This action attribute specifies which php-site should process the form.
See more here: http://www.w3schools.com/tags/att_form_action.asp

Related

PHP - How to update multiple row using foreach?

so i'm making a php file that updates multiple rows to mysql but I'm having a problem whenever i submit,and I have no idea if I'm using foreach well. here is my code:
$query = "SELECT id, departments, deptName, headOfOffice FROM aip";
$result = mysqli_query($db,$query);
$count = mysqli_num_rows($result);
while ($row = mysqli_fetch_assoc($result)) {
echo '<tr>';
echo '<td><input type="text" name="id[]" value="'.$row['id'].'" readonly></td>';
echo '<td><input type="text" id ="department_code" name="department_code[]" value="'.$row['departments'].'"></td>';
echo '<td><input type="text" id="department_name" name="department_name[]" value="'.$row['deptName'].'"></td>';
echo '<td><input type="text" id="department_head" name="department_head[]" value="'.$row['headOfOffice'].'"></td>';
echo '</tr>';
}
echo '<tr>';
echo '<td></td>';
echo '<td></td>';
echo '<td><input type="submit" name="update" value="Update">';
echo '</tr>';
if($_SERVER["REQUEST_METHOD"] == "POST"){
$deptid = $_POST['id'];
$code = $_POST['department_code'];
$dname = $_POST['department_name'];
$dhead =$_POST['department_head'];
foreach($_POST['id'] as $count){ \\ i don't know if this is right.
$query2 = "UPDATE aip SET deparments = '".$code[$count].'" WHERE id = "'.$deptid[$count]."'";
$result2 = mysqli_query($db,$query2);
}
}
the error says "Undefined offset: 2"
I'm a newbie here, and this is my first time using arrays. hope someone could help. please!
foreach($_POST['id'] as $count => $id){
$query2 = "UPDATE aip SET deparments = '".$code[$count]."' WHERE id = '".$deptid[$count]."'";
$result2 = mysqli_query($db,$query2);
}
P.S. your code is vulnerable to SQL injection
There are two ways of using foreach:
foreach($array as $key => $value){
...
}
or
foreach($array as $value){
...
}
The foreach loop will then iterate through all elements in array, executing the code in brackets for each element once, holding the key name and the value of the element in $key and $value respectively (you can use other than $value and $key as well).
I don't fully understand what exactly you are doing in your code, so if you add an explanation, I will be able to solve your particular case;
i have changed in your script..pls used my script. you do not need to changes any thing just copy and paste my script.
if($_SERVER["REQUEST_METHOD"] == "POST"){
$data = $_POST;
if(count($data['id'])>0){
foreach($data as $key=>$item){
$query2 = "UPDATE aip SET deparments = '".$item['department_code'][$key]."', deptName = '".$item['department_name'][$key]."',headOfOffice = '".$item['department_head'][$key]."' WHERE id = '".$item['id'][$key]."'";
$result2 = mysqli_query($db,$query2);
}
}
}

PHP -- Get item in array by numerical index stored in a variable?

I have a database, and I'm querying it to get all rows. Then, I get a specific record identified by a variable called id which is from somepage.php?id=aninteger
For some reason, I can't figure out how to get the result which is number id in the database. (It starts at 0).
Here is my code:
<?php
$connection = mysqli_connect("host","user","password","database");
$id = $_POST['id'];
$result = mysqli_query($connection, "SELECT * FROM Scavenger");
$resultIDArray = array();
while ($row = mysqli_fetch_array($result)) {
array_push($resultIDArray, $row['ID']);
}
$resultQArray = array();
while ($row = mysqli_fetch_array($result)) {
array_push($resultQArray, $row['Key']);
}
$resultAArray = array();
while ($row = mysqli_fetch_array($result)) {
array_push($resultAArray, $row['Answer']);
}
echo "<p>BlahBlah<br>BlahBlah<br><br><br>".$resultQArray[(int) $id]."<br><br></p>";
echo "<form action='checkAnswer.php' method='post'><input type='text' name='Answer' id='Answer'><input type='submit' value='Submit'></form>";
?>
I know there is something wrong with $resultQArray[(int) $id], but for the life of me I can't seem to figure out what.
You are walking all of your query results in your first where loop so all of the subsequent loops are failing the condition check the first time through .... try doing all of your work the first time through.
$connection = mysqli_connect("host","user","password","database");
$id = $_POST['id'];
$result = mysqli_query($connection, "SELECT * FROM Scavenger");
$resultIDArray = array()
$resultQArray = array();
$resultAArray = array();
while ($row = mysqli_fetch_array($result)) {
array_push($resultIDArray, $row['ID']);
array_push($resultQArray, $row['Key']);
array_push($resultAArray, $row['Answer']);
}
echo "<p>BlahBlah<br>BlahBlah<br><br><br>".$resultQArray[(int) $id]."<br><br></p>";
echo "<form action='checkAnswer.php' method='post'><input type='text' name='Answer' id='Answer'><input type='submit' value='Submit'></form>";
?>

Update Multiple Rows (PHP + MySQL)

I am working on a lead management system - and as the database for it grows the need for more bulk functions appears - and unfortunately I am getting stuck with one of them. The database stores many different leads - with each lead being assigned to a specific closer; thus the database stores for each lead the lead id, name, closer name, and other info. The main lead list shows a checkbox next to each lead which submits the lead id into an array:
<input type=\"checkbox\" name=\"multipleassign[]\" value=\"$id\" />
Now this all goes to the following page:
<?php
include_once"config.php";
$id = $_POST['multipleassign'];
$id_sql = implode(",", $id);
$list = "'". implode("', '", $id) ."'";
$query = "SELECT * FROM promises WHERE id IN ($list) ";
$result = mysql_query($query);
$num = mysql_num_rows ($result);
if ($num > 0 ) {
$i=0;
while ($i < $num) {
$closer = mysql_result($result,$i,"business_name");
$businessname = mysql_result($result,$i,"closer");
echo "$closer - $businessname";
echo"<br>";
++$i; } } else { echo "The database is empty"; };
echo "<select name=\"closer\" id=\"closer\">";
$query2 = "SELECT * FROM members ";
$result2 = mysql_query($query2);
$num2 = mysql_num_rows ($result2);
if ($num2 > 0 ) {
$i2=0;
while ($i2 < $num2) {
$username = mysql_result($result2,$i2,"username");
$fullname = mysql_result($result2,$i2,"name");
echo "<option value=\"$fullname\">$fullname</option>";
++$i2; } } else { echo "The database is empty"; }
echo "</select>";
?>
I want to be able to use the form on this page to select a closer from the database - and then assign that closer to each of the leads that have been selected. Here is where I have no idea how to continue.
Actually - i got it. I don't know why I didn't think of it sooner. First off I passed the original $list variable over to the new page - and then:
<?php
include_once"config.php";
$ids = $_POST['list'];
$closer = $_POST['closer'];
$query = "UPDATE `promises` SET `closer` = '$closer' WHERE id IN ($ids) ";
mysql_query($query) or die ('Error updating closers' . mysql_error());
echo "A new closer ($closer) was assigned to the following accounts:";
$query = "SELECT * FROM promises WHERE id IN ($list) ";
$result = mysql_query($query);
$num = mysql_num_rows ($result);
if ($num > 0 ) {
$i=0;
while ($i < $num) {
$businessname = mysql_result($result,$i,"business_name");
echo "<li>$businessname";
++$i; } } else { echo "The database is empty"; };
?>
The updated page before this:
$query = "SELECT * FROM promises WHERE id IN ($list) ";
$result = mysql_query($query);
$num = mysql_num_rows ($result);
if ($num > 0 ) {
$i=0;
while ($i < $num) {
$closer = mysql_result($result,$i,"business_name");
$businessname = mysql_result($result,$i,"closer");
echo "$closer - $businessname";
echo"<br>";
++$i; } } else { echo "The database is empty"; };
echo "<form name=\"form1\" method=\"post\" action=\"multiple_assign2.php\">";
echo "<input type=\"hidden\" name=\"list\" value=\"$list\" />";
echo "<select name=\"closer\" id=\"closer\">";
$query2 = "SELECT * FROM members ";
$result2 = mysql_query($query2);
$num2 = mysql_num_rows ($result2);
if ($num2 > 0 ) {
$i2=0;
while ($i2 < $num2) {
$username = mysql_result($result2,$i2,"username");
$fullname = mysql_result($result2,$i2,"name");
echo "<option value=\"$fullname\">$fullname</option>";
++$i2; } } else { echo "The database is empty"; }
echo "</select>";
echo "<input name=\"submit\" type=\"submit\" id=\"submit\" value=\"Reassign Selected Leads\">";
?>
After you select the leads and submit the form , your script should show them in a list with hidden inputs (with name=leads[] and value=the_lead's_id) and next to each lead there will be a dropdown box () which will be populated with all the closers.
After choosing and sending the second form your script will "run" all-over the leads' ids array and update each and every one of them.
Got the idea or you want some code?

Updating multiple SQL records using a single submit button

Scenario: I have multiple text boxes in which a user will enter data into some of them / all of them / or none of them.
Goal: I need to be able to UPDATE multiple records based on what is in the text boxes where the users has entered their data.
Problem: The update statement is not working when I try to update each record for each text box.
Below is the code:
$conn = mysql_connect ($localhost, $user, $pass);
mysql_select_db($db_name, $conn) or die (mysql_error());
$myFile = "/var/www/html/JG/LSP/lsp_ref.txt";
$fh = fopen($myFile, 'r');
$theData = fread($fh, 5);
fclose($fh);
if (isset($_POST['submit'])) {
foreach ($_POST['notes'] as $key=>$value) {
echo $_POST['notes'][$key];
#echo "<br/>";
//echo "$key";
//echo "<br/>";
$query_update = "UPDATE lsp_active SET Notes = ".$_POST['notes'][$key];
$result_update = mysql_query($query_update);
}
#header ('Location:lsp_display.php');
}
$query = "SELECT * FROM lsp_active";
$result = mysql_query($query);
$field_num = mysql_num_fields($result);
echo "<form method='post' action='lsp_display.php'>";
echo "<table border=1>";
$cols = 0;
while ($row = mysql_fetch_assoc($result)) {
if ( $cols == 0) {
$cols = 1;
echo "<tr>";
foreach ($row as $col => $value) {
print "<th>$col</th>";
}
print "<th>Insert Ticket / Notes</th>";
echo "</tr>";
}
echo "<tr>";
foreach ($row as $cell) {
echo "<td>$cell</td>";
}
echo "<td><input type='text' name='notes[]'/></td>";
echo "</tr>\n";
}
echo "<tr><td colspan=8><input type='submit' name='submit' value='Update'/></td></tr>";
echo "</form>";
mysql_free_result($result);
?>
Now when I print out $_POST['notes'][$key] it spits back out what I give it in the text boxes.
However, the update statement that I have for my SQL isn't updating the database with what I put in.
I am not sure what could be wrong with it :(.
Any help is appreciated!
Thank you!
It looks like you probably need to surround your $_POST in single quotes.
Also use a function to clean the $_POST variable.
For example:
function escape($data) {
$magicQuotes = get_magic_quotes_gpc();
if(function_exists('mysql_real_escape_string')) {
if($magicQuotes) {
$data = stripslashes($data);
}
$data = mysql_real_escape_string($data);
}
else {
if(!$magicQuotes) {
$data = addslashes($data);
}
}
return $data;
}
And then your query:
$query_update = "UPDATE lsp_active SET Notes = '" . escape($_POST['notes'][$key]) . "'";
Edit:
You also might want to put a WHERE statement on the end of your UPDATE query, so that you don't update everything in the table.
"UPDATE lsp_active a SET a.Notes = '" . mysql_real_escape_string($_POST['notes'][$key]) ."' WHERE a.Index = '" . ($key + 1). "'"
Index is a keyword thar refers to indexes, not your column. So I defined an alias, and made it explicit that Im referring to the column. Also, the + 1 on the Where $key since Index is not zero-based like PHP arrays.

inserting radio button in php page

I have retrieved the database details from a database to a
php page. i have actually retrieved a specific column of a query.
but i am not able to add the radio buttons to the retrieved values.
Following is my coding:
<?php
$query = "SELECT url FROM measurementurl";
$result = mysql_query($query);
while($row = mysql_fetch_row($result))
{
$url = $row[0];
echo "url :$url <br>" ;
}
?>
Try this:
<form action="">
<?php
$query = "SELECT url FROM measurementurl";
$result = mysql_query($query);
while($row = mysql_fetch_row($result))
{
//$url = $row[0]; removed cause not used in code
echo "<input type=\"radio\" name=\"url\" value=\"$row[0]\" />$row[0]<br />";
}
?>
</form>

Categories