Update Multiple Rows (PHP + MySQL) - php

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?

Related

Select filter dropdown/pagination with only php

I'm new to this site and still fairlu new to PHP so not sure if this is were I need to place my question but here goes. I am currently trying to create a filter for my table using PHP.
The filter options are already in my database; e.g I have a species column which currently contains Snake and Lizard. I have managed to get the options to display in a Dropdown list so when selected it correctly filters either Snake or Lizard, they work and filter correctly however the main problem I have is that when I select the option to progress through the filtered pages using the numbered navigation buttons it clears my filter option and shows everything in the database again. Is it possible to make lock the selection until cleared or another item is selected?
i.e. If I select Lizard from the dropdown is it possible to keep this selection until another option is selected.
Just to clarify, I am using PHP.
`
if (!empty($_POST['dropdown']) && $_POST['dropdown'] == 'snake') {
$sql = "SELECT * FROM animal where species = 'snake' and user = '$username' LIMIT $start_from,".$limit;
$all_data=mysqli_query($con,$sql);
$user_count = mysqli_fetch_row($all_data); // say total count 9
$total_records = $user_count[0]; //9
$total_pages = ceil($total_records / $limit); // 9/3= 3
}
else if(!empty($_POST['dropdown']) && $_POST['dropdown'] == 'lizard') {
$sql = "SELECT * FROM animal where species = 'lizard' and user = '$username' LIMIT $start_from,".$limit;
$all_data=mysqli_query($con,$sql); //added
$user_count = mysqli_fetch_row($all_data); // say total count 9
$total_records = $user_count[0]; //9
$total_pages = ceil($total_records / $limit); // 9/3= 3
}
else { //added
$sql = "SELECT * FROM animal where user = '$username' LIMIT $start_from,".$limit;
$all_data=mysqli_query($con,"select count(*) from animal where user = '$username'");
$user_count = mysqli_fetch_row($all_data); // say total count 9
$total_records = $user_count[0]; //9
$total_pages = ceil($total_records / $limit); // 9/3= 3
}
$num = 0;
// if($result = mysqli_query($con, $query)){
if($result = mysqli_query($con, $sql)){
if(mysqli_num_rows($result) > 0){
echo "<table>";
while($row = mysqli_fetch_array($result)){
if ($num++ % 4== 0 && $num > 1) echo '</tr><tr>';
echo "<td>" . $row['animal'] . "</td>";
// }
// echo "</tr>";
}
// echo "</table>";
}
}
$current_page = isset($_GET['page'])?$_GET['page'] : 1;
for ($page = $start_page; $page <= $end_page; $page++){
if ($total_pages > 0) {
if ($page == $current_page) {
$active_class = "active";
echo"<button class='btn' class='active' a href='room.php?page=".($page)."'>$page</a></button>";
echo " ";
}
// else {
// else if ($num == $limit){
else if ($num == $limit){
echo ''.$page.'';
echo " ";
}
}
}
echo "<form id='form_id' method='post' name='myform'>";
echo "<select name='dropdown'>";
// echo "<option value='All'>All</option>";
if($result = mysqli_query($con, $sql)){
if(mysqli_num_rows($result) > 0){
while($row = mysqli_fetch_array($result)){
echo "<option name='all'>" . $row['species'] . "</option>";
}
echo "</select>";
echo "<input id='submit' name='submit' type='submit' value='submit'>";
echo "</form>";
`
this is the grab from options display to a page into a table and a filter with pagination
Your question is not fully clear, but perhaps you are looking for the selected attribute.
<select name="species">
<option value="Snake">Snake</option>
<option value="Lizard" selected>Lizard</option>
</select>
with PHP you could do something like this:
<?php
$selectedSpecies = $_POST['species'];
?>
<select name="species">
<?php foreach (['Snake', 'Lizard'] as $species) { ?>
<option value="<?= $species; ?>" <?= $species === $selectedSpecies ? 'selected' : ''; ?>><?= $species; ?></option>
<?php } ?>
</select>

PHP new strings update in DB with unshift?

I have a problem to adding more strings in my database.
The idea is: SELECT information, then added array together, after these UPDATE to database.
These are in one code, but UPDATE not working with summed arrays only separately.
With echo I see the array_unshift is working well, the data is good, but not updating.
Need I change something on the server? Maybe version?
(I don't get mysqli_error!)
//CHECKBOX KIOLVASÁSA DB-BŐL!
$sql = ("SELECT id, checkbox FROM osszesito WHERE id = '$id'");
//$result = mysqli_query($conn, $sql);
//if (mysqli_num_rows($result) > 0) {
if ($result = mysqli_query($conn, $sql)) {
while($row = mysqli_fetch_assoc($result)) {
//EREDETI SOR LISTÁZÁSA
$original_array = array( $row["checkbox"] );
$x ="";
echo 'Eredeti sor: ';
foreach ($original_array as $x)
{
echo "$x "."<br><br>";
}
//EREDETI SOR KIEGÉSZÍTÉSE AZ ÚJ ADATTAL
array_unshift($original_array, $chb);
$last ="";
echo "Új sor: "."<br>";
foreach ($original_array as $last)
{
echo $last."<br>";
}
//ÚJ SOR FRISSÍTÉSE A DB-BEN!
//$sqla = "UPDATE osszesito SET checkbox = '$chb' WHERE id = '$id' ";
$sqla = "UPDATE osszesito SET checkbox = '$last' WHERE id = '$id' ";
if (mysqli_query($conn, $sqla)) {
echo "ÚJ SOR ELMENTVE!";
//header("Location: /megrendelesek/index.php");
} else {
echo "Hiba a beírás során: " . mysqli_error($conn);
}
}
///////////////////////////////////////////////
//LEZÁRÁS
} else {
echo "Jelenleg nincs megrendelés az adatbázisban!";
}
mysqli_close($conn);

possible ?: mysql row to an if condition

hi guys im trying to insert a mysql data to a variable that will set an if condition depending on the result. is this possible, am i doing it right? what is the right way to do it ? what i want to achieve is to validate if there's a equal value given by the user inside my mysql rows.
$db = mysql_connect('localhost','test','');
if (!$db)
{
print "<h1>Unable to Connect to MySQL</h1>";
}
$dbname = 'test';
$btest = mysql_select_db($dbname);
if (!$btest)
{
print "<h1>Unable to Select the Database</h1>";
}
$sql_statement = "SELECT * ";
$sql_statement .= "FROM registered_email ";
$result = mysql_query($sql_statement);
$outputDisplay = "";
$myrowcount = 0;
if (!$result) {
$outputDisplay .= "<br /><font color=red>MySQL No: ".mysql_errno();
$outputDisplay .= "<br />MySQL Error: ".mysql_error();
$outputDisplay .= "<br />SQL Statement: ".$sql_statement;
$outputDisplay .= "<br />MySQL Affected Rows: ".mysql_affected_rows()."</font><br />";
}
else{
$numresults = mysql_num_rows($result);
for ($i = 0; $i < $numresults; $i++)
{
$row = mysql_fetch_array($result);
$id = $row['id'];
$sentEmailClients = $row['email'];
$outputDisplay.= "".$sentEmailClients."<br />";
}
}
and here what im trying to achieve, btw is $clientEmail has a default values so dont worry about that.
if($clientEmail === $outputDisplay){
...... some codes..........
}
else{
....... some codes.......
}
you can use mysql row to compare with your user input. you can add condition, while you'r getting row value for the email inside the loop.
$email_exist = 0;//define the default value.
for ($i = 0; $i < $numresults; $i++)
{
$row = mysql_fetch_array($result);
$id = $row['id'];
$sentEmailClients = $row['email'];
$outputDisplay.= "".$sentEmailClients."<br />";
//my code start here
if($sentEmailClients == $clientEmail)
$email_exist = 1;
}
//outside the loop
if($email_exist == 1) {
//..........write some code.......
}else{
//........write some code.......
}
why don't you use a while loop?
make sure to update to mysqli_* because mysql_* is deprecated and is going to get removed on php 7.0
$email_exist = 0;//define the default value.
while ( $row = mysql_fetch_assoc($result) ) // you are using associative array and not the indexed once tho you should go for mysql_fetch_assoc
{
$id = $row['id'];
$sentEmailClients = $row['email'];
$outputDisplay.= "".$sentEmailClients."<br />";
//my code start here
if($sentEmailClients == $clientEmail)
$email_exist += 1; //maybe it exist more than once?
}
//outside the loop
if($email_exist == 1) {
//..........write some code.......
}else{
//........write some code.......
}
or you can do something more simple like this
$query = "select email from tablename where email='$clientemail'";
$result = mysql_query($query);
$count = mysql_num_rows($result);
if($count > 0) {
// email exists
} else {
// doesn't exist
}

Dropdown of tables to show content

This script allows the user to choose a table from the dropdown and then displays the contents of the chosen table.
At the moment the DB connection is working.
A list of tables is shown in the dropdown.
Problem: No content from the database table is shown.
I have checked through the code and everything looks OK, but it still only seems to partially work.
<?php
//update this to your DB connection details.
$dbh = "localhost";
$dbn = "dbname";
$dbu = "dbuser";
$dbp = "dbpass";
$conn = mysql_connect($dbh,$dbu,$dbp) or die("Unable to connect do database.");
mysql_select_db($dbn, $conn) or die("Unable to select database.");
//Some vars for Order by and Limit.
if (!isset($ordBy)){
$ordBy = 1;
}
if (!isset($ps)){
$ps = 0;
}
if (!isset($ord)){
$ord = 1;
}
if ($ord == 1){
$tOrder = "ASC";
} else {
$tOrder = "DESC";
}
//Tables drop-down
$result = mysql_query("SHOW TABLES FROM $dbn") or die("Cannot list table names.");
echo "
<form name=\"table_browser\" action=\"".$PHP_SELF."\" method=\"GET\" >
<select name=\"t\" onChange=\"javascript:submit();\">
<option>Select a table</option>
";
while ($row = mysql_fetch_row($result)){
echo " <option value=".$row[0].">".$row[0]."</option>\n";
}
echo " </select>
</form>\n";
if (!isset($t)){
die("Please select a table");
}
//Get number of rows in $t and then select
$result = mysql_query("SELECT COUNT(*) FROM $t") or die("The requested table doesn't exist.");
$total = mysql_result($result,0);
$qry = "SELECT * FROM $t ORDER BY ".$ordBy." ".$tOrder." LIMIT ".($ps*20).",20 ";
if (isset($qry)) {
$result = mysql_query($qry) or die("The requested table doesn't exist.");
$i = 0;
while ($i < mysql_num_fields($result)) {
$meta = mysql_fetch_field($result);
if (!$meta) {
echo "No information available on the table<br />\n";
}
$name[$i] = $meta->name;
$i++;
}
//Display table details
echo "Rows ".($ps*20+1)." to ".((($ps+1)*20 < $total) ? (($ps+1)*20) : ($total))." of $total from table:
<b>$meta->table</b>\n<br /><br />\n";
//Count results
if ($ps > 0) {
echo "20 Previous - ";
} else {
echo "20 Previous - ";
}
if ((($ps+1)*20) < $total ){
echo "Next 20\n";
} else {
echo "Next 20\n";
}
//Column names
echo "<br /><br />\n<table>\n <tr>\n";
for ($j = 0; $j < $i; $j++){
echo " <td><b>$name[$j]</b>";
if ($ordBy == $name[$j]) {
echo "<img src=\"images/arrow$ord.gif\">";
}
echo "</td>\n";
}
echo " </tr>\n";
//Data
while ($row = mysql_fetch_array($result)){
echo " <tr onmouseover=\"this.style.background='#DDDDDD'\" onmouseout=\"this.style.background=''\">";
for ($j = 0; $j < $i; $j++){
echo "<td>".$row[$name[$j]]."</td>";
}
echo "</tr>\n";
}
echo "</table>\n";
}
mysql_close();
?>
#gentlebreeze - my eyes hurt from trying to read that - but I can't see where you are actually setting the variable called $t - which is used to determine the table. You should have
$t = $_GET['t'];
somewhere before the line:
....if (!isset($t)){....
because you need to use it in the line:
....$result = mysql_query("SELECT COUNT(*) FROM $t"....
and you should not be using mysql_query either - old and now deprecated. You should switch to PDO and bound variables.

PHP Script, Table data populating from form

<html><head></head><body>
<?php
$db_hostname = "mysql";
$db_database = "u1da";
$db_username = "u1da";
$db_password = "1234";
$con = mysql_connect($db_hostname ,$db_username ,$db_password);
if (!$con) die ("Unable to connect to MySQL: ".mysql_error ());
mysql_select_db ( $db_database ) ||
die (" Unable to select database : ". mysql_error());
$query = "select * from students";
$result = mysql_query($query);
$result || die ("Database access failed: ".mysql_error());
$rows = mysql_num_rows($result);
echo "<table border=1>";
echo "<tr><td><p><b>Name</b></p></td></tr>";
for ($j = 0; $j < $rows ; $j ++) {
echo "<tr><td>", mysql_result($result,$j,'name') ,"</td></tr>";
}
echo "</table><br />";
$query2 = "select * from groups";
$result2 = mysql_query($query2);
$result2 || die ("Database access failed: ".mysql_error());
$rows2 = mysql_num_rows($result2);
echo "<table border=1>";
echo "<tr><td><p><b>Tutorial Group</b></p></td><td><p><b>Capacity</b></p></td></tr>";
for ($i = 0; $i < $rows2 ; $i ++) {
echo "<tr><td>", mysql_result($result2,$i,'Tutorial_Group') ,"</td><td>", mysql_result($result2,$i,'Capacity') ,"</td></tr>";
}
echo "</table>";
echo "<form method='post' action='' enctype="multipart/form-data">";
$query3 = "select * from students";
$result3 = mysql_query($query3);
echo "<br /><br /><select name='name'>";
while($name = mysql_fetch_array($result3)) {
echo "<option value='$name[Name]' > $name[Name] </option>"."<BR>";
}
echo "</select><br />";
$query4 = "select Tutorial_Group from groups";
$result4 = mysql_query($query4);
echo "<select name = 'group'>";
while($grp = mysql_fetch_array($result4)){
echo "<option value='$grp[Tutorial_Group]'>$grp[Tutorial_Group]</option>";
}
echo "</select><br />";
echo "Student ID: ";
echo '<input type="text" name="SID"><br />';
echo "Email: ";
echo '<input type="text" name="email"><br />';
echo '<input type="submit" name="submit" value="Submit">';
echo "</form>";
?>
Here is my current script code.
I have to make a query which will get the value from the 2 drop-down menus and 2 text fields and insert them into a table.
Table is called assg and have columns: Name, Student_ID, email, s_group. I have tried some different ways, but it didn't worked out. Please help.
Your first problem is that you are using mysql_fetch_array to get an array of elements into $grp variable but next you try to use it with a associative array to get values like $grp[Tutorial_Group]
Using mysql_fetch_array you can get $grp[0] ... $grp[1] but not $grp[Tutorial_Group]
You need to use mysql_fetch_assoc to get associative arrays like $grp[Tutorial_Group]
Your second problem is using complex php vars incorrectly example
Incorrect:
echo "<option value='$grp[Tutorial_Group]'>$grp[Tutorial_Group]</option>";
Correct:
echo '<option value="'.$grp['Tutorial_Group'].'">'.$grp['Tutorial_Group'].'</option>';
Or
echo "<option value='{$grp['Tutorial_Group']}'>{$grp['Tutorial_Group']}</option>";
Also the code only has the part to view the tables and the form. The insert part of the data is missing in the example. Probably this part has also some errors.
No indentation, html in echos, i don't even understand what you want.
By dropdown you mean select ?
What about $_POST['nameOfTheField'] ?
And why multipart ? There's no files to send here.

Categories