PHP + MySQL - Form cannot update multiples tables at a time - php

I'm having bad time to update multiple tables from a form. I've done the query and checked for errors using (or die), but it seems that there are no errors in my MySQL codes. I can update the main table's data, but I can't update the other table. I'm suspecting that my form fields have some problem. This is my form codes :
<?php
$sql= "SELECT * FROM client WHERE resID=".$_GET["resID"];
$rs = mysql_query($sql) or die($sql."<br/><br/>".mysql_error());
$sqlM= "SELECT * FROM menu WHERE resID=".$_GET["resID"];
$rsM = mysql_query($sqlM) or die($sqlM."<br/><br/>".mysql_error());
$i = 0;
echo '<table width="50%">';
echo '<tr>';
echo '<td>ID</td>';
echo '<td>Name</td>';
echo '<td>Edit</td>';
echo '</tr>';
echo "<form name='form_update' method='post' action='client_admin_post.php'>\n";
$f=mysql_fetch_array($rs);echo '<tr>';
echo "<td>Res ID :</td>";
echo "<td>{$f['resID']}<input type='hidden' name='resID' value='{$f['resID']}' /></td>";
echo '</tr>';
++$i;
echo '<tr>';
echo "<td>Restaurant Name :</td>";
echo "<td><input type='text' size='40' name='resName' value='{$f['resName']}' /></td>";
echo '</tr>';
++$i;
while ($fM = mysql_fetch_array($rsM)) {
echo '<tr>';
echo "<td>Menu :</td>";
echo "<td><input type='text' size='40' name='mname[$i]' value='{$fM['name']}' /></td>";
echo "<td>{$fM['id']}<input type='hidden' name='mid[$i]' value='{$fM['id']}' /></td>";
echo '</tr>';
++$i;
}
echo '<tr>';
echo "<td><input type='submit' value='submit' /></td>";
echo '</tr>';
echo "</form>";
echo '</table>';
?>
This is my POST codes :
<?php
//session_start();
include_once("connection.php");
$resID= $_POST["resID"];
$resName= $_POST["resName"];
$sql = "UPDATE client ".
"SET resName = '$resName' ".
"WHERE resID = '$resID' " ;
mysql_query($sql) or die ('query failed:' . mysql_error());
$size = count($_POST['mname']);
$i = 0;
while ($i < $size) {
$name= $_POST['mname'][$i];
$id = $_POST['mid'][$i];
$sqlM = "UPDATE menu SET name = '$name' WHERE id = '$id' LIMIT 1";
mysql_query($sqlM) or die ("Error in query: $sqlM");
echo "$name<br /><br /><em>Updated!</em><br /><br />";
++$i;
}
?>
As you guys know, one restaurant got many menus. I can edit the main restaurant info, but I can't edit the menus. Please help me. Really appreciate your help. Thanks :D

You ++$i at least twice before outputing it as $mname index. So $_POST['mname'] would have indices 2 .. count_of_rows+1. Your while() ignores this fact and counts 0 .. size.
If you had all warnings turned on (as you should) you would get a warning indicating that invalid index 0 is used on line starting "$name= $_POST..." and you would have been able to figure out from there.

Related

PHP dynamic table

I have to make a dynamic table n*n , the user first gives the number n and the program makes a 5*5 table with check box this part I have make it, the second part is the user checks same of the checkbox and clicks on submit and the program makes again a table 5*5 but in the place of check box which checks is colored. I have uploaded and image.
Sorry for my bad English, thanks for your time.
enter image description here
<form name="form" action="" method="get">
<input type="text" name="subject" id="subject" value="Give value">
</form>
<?php
$rows = $cols = $name = "";
if(isset($_GET['subject']))
$rows = $cols = $_GET['subject'];
if(isset($_POST['check_list']))
$name = $_POST['check_list'];
if(isset($_GET['subject'])){
echo "<form action='my.php' method='post'>";
echo "<table border='1'>";
for($tr=1;$tr<=$rows;$tr++){
echo "<tr>";
for($td=1;$td<=$cols;$td++){
echo "<td><input type='checkbox' name='check_list[]' value='value ".$td."'></td>";
}
echo "<tr>";
}
echo "</table>";
echo "<input type='submit' />
</form>";
}
// this part of code is not make the third excecution the number 3 image
echo $cols;
echo "<table border='1'>";
for($tr=1;$tr<=$rows;$tr++){
echo "<tr>";
foreach($_POST['check_list'] as $value){
if($tr == $value[td])echo "<td bgcolor='#FF0000'></td>";
else
echo "<td> </td>";
}
echo "</tr>";
}
echo "</table>";
?>

Adding delete button to php code displaying mysql table contents

I have searched for an answer that relates to my code but I'm new to php and html so I would appreciate the help.
So I've created code that uses loops to display a html table with the contents of the specified table from a mysql database. I would like to give the user an option to delete a row when the results are displayed.
This is my code so far to display the results of my database table:
<html><head><title>MySQL Table Viewer</title></head><body>
<?php
$db_host = 'localhost';
$db_user = 'username';
$db_pwd = 'password';
$database = 'dvdproject';
$table = 'Employee';
$con= mysql_connect($db_host,$db_user,$db_pwd,$database);
if(!$con){
die("Can not connect" . mysql_error());
}
mysql_select_db($database,$con);
// sending query
$result = mysql_query("SELECT * FROM {$table}");
$fields_num = mysql_num_fields($result);
echo "<h1>Table: {$table}</h1>";
echo "<table border='1'><tr>";
// printing table headers
for($i=0; $i<$fields_num; $i++)
{
$field = mysql_fetch_field($result);
echo "<td>{$field->name}</td>";
}
echo "</tr>\n";
// printing table rows
while($row = mysql_fetch_row($result))
{
echo "<tr>";
foreach($row as $cell)
echo "<td>$cell</td>";
echo '<form method="POST" name="deleterequest" action =
"deleterequest.php">';
echo "<input name='record_id' type='hidden' value='".$row['id']."'
>";
echo "<input name='delete'type='submit' value='Delete' >";
echo "</form>";
echo "</tr>\n";
}
echo "</table>";
mysql_close($con);
?>
No matter where I place my form to delete button, 6 delete buttons take up an entire row followed by the actual rows.
I would like the delete button to be after each row but I just cant get my head around it!
Apologies if I am overlooking something here, but have you tried wrapping your delete button with <td> tags?
foreach($row as $cell)
echo "<td>$cell</td>";
echo "<td>";
echo '<form method="POST" name="deleterequest" action="deleterequest.php">';
echo "<input name='record_id' type='hidden' value='".$row['id']."'>";
echo "<input name='delete'type='submit' value='Delete' >";
echo "</form>";
echo "</td>";
echo "</tr>\n";
}
I stripped everything bar HTML away from this and found a <td> tag around the form worked a treat.

php multiple search textbox with one submit button

i would like to know how can i make a multiple search criteria with 2 or more textboxes and only one submit button.
my script is:
$sql = "select * from wp_studenti ";
if (isset($_POST['search'])) {
$search_term = mysql_real_escape_string($_POST['search_box']);
$sql .= " WHERE nume= '{$search_term}' ";
}
$query = mysql_query($sql) or die (mysql_error());
echo "<form name ='search_form' method='POST' action='search.php'>";
echo "<center><h3>Cauta:</h3> <input type='text' name='search_box' />";
echo "<input type='submit' name='search' value='Cauta' /></center>";
echo "</form>";
and my results page that shows after search page:
$sql = "select * from wp_studenti ";
if (isset($_POST['search'])) {
$search_term = mysql_real_escape_string($_POST['search_box']);
$sql .= "WHERE nume= '{$search_term}'";
}
echo "<center>\n";
echo "<table border='1'>";
echo "<thead>";
echo "<tr><th>Id</th>";
echo "<th>Nume</th>";
echo "<th>Localitate</th>";
echo "<th>Judet</th>";
echo "<th>Sector Financiar</th>";
echo "<th>Link</th></tr>";
echo "</thead>";
$rst = mysql_query($sql);
while($a_row = mysql_fetch_assoc($rst)) {
echo "<tr>";
echo "<td>"; echo $a_row['id']; echo "</td>";
echo "<td>"; echo $a_row['nume']; echo "</td>";
echo "<td>"; echo $a_row['localitate']; echo "</td>";
echo "<td>"; echo $a_row['judet']; echo "</td>";
echo "<td>"; echo $a_row['sector_financiar']; echo "</td>";
echo "<td>"; echo "<a href='results.php?id={$a_row['id']}'>{$a_row['link']}</a>" ; echo "</td>";echo "</tr>";
echo "</table>";
$sql = "select * from wp_studenti ";
if (isset($_POST['search'])) {
$search_term_by_Cauta = mysql_real_escape_string($_POST['search_box_1']);
$search_term_by_localitate = mysql_real_escape_string($_POST['search_box_2']);
//If you want both search mandatory, use "AND" Operator otherwise use "OR". If you want approximate search use "LIKE" Operator in bellow SQL
$sql .= " WHERE nume= '{$search_term_by_Cauta }' OR localitate = '{$search_term_by_localitate }' ";
}
$query = mysql_query($sql) or die (mysql_error());
echo "<form name ='search_form' method='POST' action='search.php'>";
echo "<center><h3>Cauta:</h3> <input type='text' name='search_box_1' />";
echo "<h3>localitate:</h3> <input type='text' name='search_box_2' />";
echo "<input type='submit' name='search' value='Cauta' /></center>";
echo "</form>";
Well you need another search box:
echo "<center><h3>Cauta:</h3> <input type='text' name='search_box1' /><input type='text' name='search_box2' />";
And you need to use that value in your SQL:
if (isset($_POST['search'])) {
$search_term1 = mysql_real_escape_string($_POST['search_box1']);
$search_term2 = mysql_real_escape_string($_POST['search_box2']);
$sql .= " WHERE nume= '{$search_term1}' OR nume= '{$search_term2}'";
}
But you will have to do some thinking about how the search should work, is it supposed to match exactly one OR the other? If you want the text to contain instead of exactly match, you can use the syntax nume LIKE '%searchword%'
Use mysqli instead of mysql, which is depreciated. By PHP, something like this;
<form method='post'>
<input type='hidden' name='srch' val='1'>
Search Type1: <input type='text' name='s1'>
<br>
Search Type2: <input type='text' name='s2'>
<button>Submit</button>
</form>
<?php
if(isset($_POST['srch']))
{
if(!empty($_POST['s1']))$search = $_POST['s1'];
else if(!empty($_POST['s2']))$search = $_POST['s2'];
else die ('No criteria entered');
rest of your code...
}
?>
Also see functions like mysqli_real_escape for security reasons.

display quantity field in table in php

I'm trying to get my table to look like this
http://gyazo.com/6a134b723c30e97fa99559158cde4b1e
but when I use my code looks like this
http://gyazo.com/1c08f83e744b3a20c99b55eee5313045
I can't get the quantity field or add field to work for the life of me; the quantity field is just blank. I would greatly appreciate some help because I can't get it to work.
<?php require_once("include/db_connect.php"); ?>
<html>
<head><title>Displaying Image files using PHP</title></head>
<body>
<h1>Displaying Images from an Image table using PHP</h1>
<?php
$db_link = db_connect("project");
// Retrieve table properties
$fields = mysql_list_fields("project", "bookstore");
$num_columns = mysql_num_fields($fields);
// Make a simple database query to select all columns and rows
$query = "SELECT * FROM bookstore";
$result = mysql_query($query) or die("SQL query failed");
// Display results as an HTML table. Note how mysql_field name
// uses the $fields object to extract the column names
echo '<table border="1" cellpadding = "5" >';
// Display the column names
echo "<tr>";
for ($i = 0; $i < $num_columns; $i++)
{
echo "<th>", mysql_field_name($fields, $i), "</th>";
}
echo "<th>"Quantity "</th>";
echo "</tr>";
// Loop over the rows of the table.
// $row contains the information for each row
// Refer to the names of the fields in the table
// Must ahow the path where the image files are held
while ($row = mysql_fetch_assoc($result))
{
echo "<tr>
<form action='somethingToHandleForm.php' method='post'>";
echo "<td>". $row['isbn']. "</td>";
echo "<td>". $row['title']."</td>";
echo "<td>". $row['author']."</td>";
echo "<td>". $row['pub']."</td>";
echo "<td>". $row['year']."</td>";
echo "<td>". $row['price']."</td>";
echo "<td><input type='text' name='quantity' value=".$row['quantity']."/></td>";
echo "<td><input type='submit' value='add></td>";
echo "</form>";
echo "</tr>";
}
echo "</table>";
// Free the resources and close the connection
mysql_free_result($result);
mysql_close($db_link);
?>
</body>
</html>
One thing I noticed is you're missing a single quote in this line after value=add:
<input type='submit' value='add>
You have some formatting problems in your HTML, which look to be the source of your problems when rendering the table. Specifically the lines:
echo "<td><input type='text' name='quantity' value=".$row['quantity']."/></td>";
echo "<td><input type='submit' value='add></td>";
The value attribute for the first line is not properly enclosed in quotes, and there is no end single-quote for the value add in the second line (nor are you closing the input tag), try the following:
echo "<td><input type='text' name='quantity' value='".$row['quantity']."' /></td>";
echo "<td><input type='submit' value='add' /></td>";

getting checked value from checkbox thats taking data from database

I have problem to get to the checked value in checkbox.
I have first query that is taking names of the tables in database and second query that is taking names of the columns of that tables. Names of the columns are displayed in a checkbox, but i have problem to take that values from the checkbox. I know I should use $_POST['kolona'] but somehow that variable its not recognized:
if(mysqli_connect_errno())
{
echo "Error: Greška u konekciji";
exit;
}
$sql1 = "SHOW TABLES FROM db_baza";
$result1 = mysql_query($sql1);
if (!$result1)
{
echo "DB Error, could not list tables\n";
echo 'MySQL Error: ' . mysql_error();
exit;
}
while ($row1 = mysql_fetch_row($result1))
{
echo "<div id='blok' style='width:200px;height:200px;background-color:red;margin:20px;float:left;'>{$row1[0]}"."</br>";
$tablename=$row1[0];
$sql2="SELECT column_name FROM information_schema.columns WHERE table_name = '{$tablename}'";
$result2= mysql_query($sql2);
while($row2=mysql_fetch_row($result2))
echo "<form id='forma1' action='' method='POST'><input type='checkbox' name='kolona[]' value='{$row2[0]}' />{$row2[0]}</br>";
echo "<input type='submit' name='submit' value='{$tablename}'/></form>";
echo "</div>";
}
if(isset($_POST['submit']))
if(isset($_POST['kolona'])
echo 1;
?>
So, I am just trying to echo number one, i will need it for other stuff but just for the example.
Modify these lines:
echo "<form id='forma1' action='' method='POST'>";
while($row2=mysql_fetch_row($result2)) {
echo "<input type='checkbox' name='kolona[]' value='{$row2[0]}' />{$row2[0]}</br>";
}
echo "<input type='submit' name='submit' value='{$tablename}'/></form>";
echo "</div>";

Categories