I got a php form to update MySQL tables. The fetch works perfectly but the updates are not working. This is my form code :
<?php
$sql= "SELECT client.resID AS resID, client.resName AS resName FROM client WHERE client.resID =".$_GET["resID"];
$rs = mysql_query($sql) or die($sql."<br/><br/>".mysql_error());
$sqlM= "SELECT menu.id AS mid, menu.name AS mname FROM menu WHERE menu.resID =".$_GET["resID"];
$rsM = mysql_query($sqlM) or die($sqlM."<br/><br/>".mysql_error());
$sqlF= "SELECT facilities.id AS fid, facilities.name AS fname FROM facilities WHERE facilities.resID =".$_GET["resID"];
$rsF = mysql_query($sqlF) or die($sqlF."<br/><br/>".mysql_error());
$sqlS= "SELECT services.id AS sid, services.name AS sname FROM services WHERE services.resID =".$_GET["resID"];
$rsS = mysql_query($sqlS) or die($sqlS."<br/><br/>".mysql_error());
// $names array now contains all names
$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";
while ($fm = mysql_fetch_array($rsM)) { // loop as long as there are more results
$mnames[] = $fm['mname'];
$mid[] = $fm['mid']; // push to the array
echo '<tr>';
echo "<td>Menu :</td>";
echo "<td><input type='text' size='40' name='mname' value='{$fm['mname']}' /></td>";
echo "<td>{$fm['id']}<input type='hidden' name='mid' value='{$fm['mid']}' /></td>";
echo '</tr>';
++$i;
print_r($mnames);
}
while ($ff = mysql_fetch_array($rsF)) { // loop as long as there are more results
$fnames[] = $ff['fname'];
$fid[] = $ff['fid']; // push to the array
echo '<tr>';
echo "<td>Facilities :</td>";
echo "<td><input type='text' size='40' name='fname' value='{$ff['fname']}' /></td>";
echo "<td>{$ff['id']}<input type='hidden' name='fid' value='{$ff['fid']}' /></td>";
echo '</tr>';
++$i;
}
while ($fs = mysql_fetch_array($rsS)) { // loop as long as there are more results
$snames[] = $fs['sname'];
$sid[] = $fs['sid']; // push to the array
echo '<tr>';
echo "<td>Services :</td>";
echo "<td><input type='text' size='40' name='sname' value='{$fs['sname']}' /></td>";
echo "<td>{$fs['id']}<input type='hidden' name='sid' value='{$fs['sid']}' /></td>";
echo '</tr>';
++$i;
}
echo'<tr>
<td colspan="3" align="center"><input type="submit" name="Submit" value="Submit"></td>
</tr>
</table>
</form>';
?>
This is my post code :
$size = count($_POST['mname']);
$i = 0;
while ($i < $size) {
$mname= $_POST['mname'][$i];
$mid = $_POST['mid'][$i];
$query = "UPDATE menu SET name = '$mname' WHERE id = '$mid' LIMIT 1";
mysql_query($query) or die ("Error in query: $query");
echo "$mname<br /><br /><em>Updated!</em><br /><br />";
++$i;
}
$size = count($_POST['fname']);
$i = 0;
while ($i < $size) {
$fname= $_POST['fname'][$i];
$fid = $_POST['fid'][$i];
$query1 = "UPDATE facilities SET name = '$fname' WHERE id = '$fid' LIMIT 1";
mysql_query($query1) or die ("Error in query: $query1");
echo "$fname<br /><br /><em>Updated!</em><br /><br />";
++$i;
}
$size = count($_POST['sname']);
$i = 0;
while ($i < $size) {
$sname= $_POST['sname'][$i];
$sid = $_POST['sid'][$i];
$query3 = "UPDATE services SET name = '$sname' WHERE id = '$sid' LIMIT 1";
mysql_query($query3) or die ("Error in query: $query3");
echo "$sname<br /><br /><em>Updated!</em><br /><br />";
++$i;
}
I got 'updated' status in post page but nothing is updated in MySQL table. How to solve this problem? Really appreciate your help :D
You're creating multiple inputs with the same name, such as "fname," but you're trying to access them as if they were an array. Rename the fields like the following:
echo "<td><input type='text' size='40' name='mname[]' value='name' /></td>";
too long question but if you but by update status in post page you mean to say it echoed update if i am right it will echo update since you are checking it in while and the condition is true. But have you echoed the posted values in post page check whether you are getting the value proceed this way to find where are you having problems before asking here long questions.
you can use transacrions in mysql if you want all the queries should be executed otherwise none.
Your post page says updated because at the end of your queries you just echo "Updated", there is no condition when it should be printed.
and your data is not updated as you are not specifying any connection variable for mysql_query("query",$conn)
Related
From the below code, if it displays 10 records and if the user changes 'Code' or 'Number' value of 2nd and 5th record, how to insert only that 2nd and 5th record in another separate table. But from my insert query i can insert all 10 records. But i need only two affected records to be inserted. Please help.
include('DB.php');
$sql="SELECT * FROM customer where customer_name='".$q."' ";
$result = mysql_query($sql);
$num_row = mysql_num_rows($result);
$row_count=1;
if($num_row>0)
{
echo "<table >
<tr>
<th>S.No</th>
<th>Name</th>
<th>Code</th>
<th>Number</th>
</tr>";
while($row = mysql_fetch_array($result)) {
$c_name=$row['c_name'];
$c_code=$row['c_code'];
$c_number=$row['c_number'];
echo "<tr>";
echo "<td> $row_count.</td>";
echo "<td><input type='text' name='c_name[]' id='c_name' value='$c_name' readonly /></td>";
echo "<td><input type='text' name='c_code[]' id='c_code' value='$c_code' /></td>";
echo "<td><input type='text' name='c_number[]' id='c_number' value='$c_number' /></td>";
echo "</tr>";
$row_count++;
}
echo "</table>";
}
INSERT:
$c_name=$_POST['c_name'];
$c_code=$_POST['c_code'];
$c_number=$_POST['c_number'];
if(isset($c_code))
{
for($i=0;$i<count($c_code);$i++)
{
{
$insert=mysql_query("INSERT INTO customer_table2(name,code,number)
VALUES ('$c_name[$i]','$c_code[$i]','$c_number[$i]')");
}
}
Do a SELECT query first, to see if the row is already in the original table. If not, add it to the new table.
$c_name=$_POST['c_name'];
$c_code=$_POST['c_code'];
$c_number=$_POST['c_number'];
if(isset($c_code))
{
for($i=0;$i<count($c_code);$i++)
{
// Prevent SQL injection
$name = mysql_real_escape_string($c_name[$i]);
$code = mysql_real_escape_string($c_code[$i]);
$number = mysql_real_escape_string($c_number[$i]);
$select = mysql_query("SELECT COUNT(*) AS c FROM customer WHERE name = '$name' AND code = '$code' AND number = '$number'");
$row = mysql_fetch_assoc($select);
if ($row['c'] == 0) {
$insert=mysql_query("INSERT INTO customer_table2(name,code,number)
VALUES ('$name','$code','$number')");
}
}
}
I have a "racing" website where I am recording racers results, but I am trying to enter their results in all at once. shown below is the code
Your problem is that you believe when you have multiple <input name="" /> you think that creates a array $_POST[], that is not true...
while ($row = mysql_fetch_assoc($result))
{
$reID = $row['reID'];
$racerID = $row['racerID'];
echo "<tr>";
echo "<td>$reID<input type='hidden' name='reID' value='$reID'>";
echo "<td>$racerID<input type='hidden' name='racerID' value='$racerID'>";
echo"<td><input type='text' name='rank'>";
echo"<td><input type='text' name='timetaken'>";
}
you are over writing the reID, racerID, rank, and time taken every time it is printed out... by default it takes the last one.
what you could do to solve this is:
$count = 0;
while ($row = mysql_fetch_assoc($result))
{
$count++;
$reID = $row['reID'];
$racerID = $row['racerID'];
echo "<tr>";
echo "<td>$reID<input type='hidden' name='reID$count' value='$reID'>";
echo "<td>$racerID<input type='hidden' name='racerID$count' value='$racerID'>";
echo"<td><input type='text' name='rank$count'>";
echo"<td><input type='text' name='timetaken$count'>";
}
echo "<input type='hidden' name='count' value='$count' />";
That way each value is unique...
Then instead of this:
$rank=$_POST['rank'];
$timetaken=$_POST['timetaken'];
$reID=$_POST['reID'];
$racerID=$_POST['racerID'];
$count_racerID= count($_POST['racerID']);
do this
$count = $_POST['count'];
for($i=1; $i<=$count; $i++){
$rank[] = $_POST['rank'.$i];
$timetaken[] = $_POST['timetaken'.$i];
$reID[] = $_POST['reID'.$i];
$racerID[] = $_POST['racerID'.$i];
}
$count_racerID = $count;
now you have arrays!
For your SQL statement...
$sql = mysql_query("INSERT INTO RaceResults (rank, timetaken, reID, racerID) VALUES ('$rank', '$timetaken', '$reID', '$racerID')");
should be
$sql = mysql_query("INSERT INTO RaceResults (rank, timetaken, reID, racerID) VALUES ('$_rank', '$_timetaken', '$_reID', '$_racerID')");
The final product should be something like this:
The input form:
$reID = $_GET['reID'];
$result = mysql_query("SELECT * FROM RaceEventRacer WHERE reID = $reID");
$count = 0;
while ($row = mysql_fetch_assoc($result))
{
$count++;
$reID = $row['reID'];
$racerID = $row['racerID'];
echo "<tr>";
echo "<td>$reID<input type='hidden' name='reID$count' value='$reID'>";
echo "<td>$racerID<input type='hidden' name='racerID$count' value='$racerID'>";
echo"<td><input type='text' name='rank$count'>";
echo"<td><input type='text' name='timetaken$count'>";
}
echo "<input type='hidden' name='count' value='$count' />";
and the SQL should be:
$count = $_POST['count'];
for($i=1; $i<=$count; $i++){
$rank[] = $_POST['rank'.$i];
$timetaken[] = $_POST['timetaken'.$i];
$reID[] = $_POST['reID'.$i];
$racerID[] = $_POST['racerID'.$i];
}
$count_racerID = $count;
for($i=0;$i<$count_racerID;$i++){
$_rank= mysql_escape_string($rank[$i]);
$_timetaken= mysql_escape_string($timetaken[$i]);
$_reID= mysql_escape_string($reID[$i]);
$_racerID= mysql_escape_string($racerID[$i]);
$sql = mysql_query("INSERT INTO RaceResults (rank, timetaken, reID, racerID) VALUES ('$_rank', '$_timetaken', '$_reID', '$_racerID')");
$result = mysql_query($sql);
}
I have 2 textbox array,
questions & correct answer...
the problem is , i cannot save it to the the database
questions should be saved to "test" field in database
and correct answer should be saved to "test2" field...
code for the textbox
echo "<label for='textfield[]' align='left'> Question </label>";
echo "<br/>";
echo "<input type='text' name='textfield[]'>";
echo "<br/>";
echo "<label for='textfield2[]' align='left'> Correct Answer </label>";
echo "<br/>";
echo "<input type='text' name='textfield2[]'>";
echo "<br/>";
code for inserting values in database
$sql = array();
foreach($_POST['textfield'] as $textfield){
foreach($_POST['textfield2'] as $textfield2){
$sql[] = "INSERT INTO practice (test,test2) VALUES ('{$textfield}','{$textfield2}')";
}
}
foreach($sql as $query){
mysqli_query($con,$query);
}
}
You can insert like this,
<?php
$post_count = count($_POST['textfield']);
$post1 = array();
$post2 = array();
$post1 = $_POST['textfield'];
$post2 = $_POST['textfield2'];
for ($i = 0; $i <= $post_count; $i++) {
$sql[] = "INSERT INTO practice (test,test2) VALUES ('".$post1[$i]."','".$post2[$i]."')";
}
foreach ($sql as $query) {
mysqli_query($con, $query);
}
?>
I'm having problems to find how to create an hyperlink in a table column result and then, on click, open another page with all fields (textboxes) filled. Imagine when a click an ID, i do a select * from table where column_id = ID... Is there a way to do it?
Thanks.
Best regards
I'm not completely sure what you are asking, but this may help you a bit.
First make a Javascript.
<script type="text/JavaScript">
function selectID() {
var ID = document.getElementById("ID").value;
document.location.href ="yoursite.php?ID="+ID;
}
</script>
Then connect to your database to query the table for a link ID (or more) for example by changing the variable $value.
<?php
//Connect to database
mysql_connect("host", "user", "pass");
mysql_select_db("db_name");
$value = 'something';
$ID = $_GET['ID'];
if (!$ID) {
$ID = 0;
}
if ($ID == 0) {
$query = "SELECT * FROM table WHERE `column_1` = '$value'";
$result = mysql_query($query);
echo "<table>";
while($myrow = mysql_fetch_array($result)) {
echo "<tr>";
echo "<td>";
echo "ID";
echo "</td>";
echo "</tr>";
}
echo "</table>";
}
elseif ($ID > 0) {
$query2 = "SELECT * FROM table WHERE `column_id` = '$ID'";
$result2 = mysql_query($query2);
while($myrow2 = mysql_fetch_array($result2)) {
$value1 = $myrow2['column_1'];
$value2 = $myrow2['column_2'];
}
echo "<form type=\"GET\" action=\"$PHP_SELF\">";
echo "<input type=\"text\" id=\"ID\" name=\"ID\" value=\"$ID\"><br>";
echo "<input type=\"text\" id=\"value1\" name=\"value1\" value=\"$value1\"><br>";
echo "<input type=\"text\" id=\"value2\" name=\"value2\" value=\"$value2\"><br>";
echo "<input type=\"hidden\" id=\"search\" name=\"search\" value=\"searching\">";
echo "<input type=\"submit\" id=\"submitbutton\" name=\"submitbutton\" value=\" Search \">";
echo "</form>";
}
?>
Trying to Update the MySQL Tables but nothing is being updated, I'm sure I'm just not seeing the tiny issue, would love some help. Thanks
So its a trade block for a hockey pool, if you want the player on the trade block then you just check the CHECKBOX in the form and submit and it should change the value in the database to value of "1".
FORM:
echo "<table border='1'>";
echo "<tr><th>NAME</th> <th>POS</th> <th>BLOCK</th></tr>";
$counter = 1;
while($row = mysql_fetch_array( $result )) {
echo "<tr><td>";
echo "{$row['f_name']}" . " " . "{$row['l_name']}";
echo "</td><td><input name='pl_id[$counter]' type='hidden' value='{$row['pl_id']}'>";
echo "{$row['pos']}";
echo "</td><td><input name='pos[$counter]' type='hidden' value='{$row['pos']}'>";
echo "<input type='checkbox' name='block[$counter]' size='1' value='1'";
if($row['block'] == '1')
{
echo "checked='checked'";
}
echo "></td></tr>";
$counter++;
}
echo "</table>";
SUBMIT PHP PAGE:
mysql_connect("localhost", "user", "pass") or die(mysql_error());
mysql_select_db("mbbcom1_fantrax") or die(mysql_error());
$i = 1;
while ($i < 26) {
$block = $_POST['block'][$i];
$pl_id = $_POST['pl_id'][$i];
$query = mysql_query("UPDATE 'players'
SET `block` = '$block'
WHERE `players`.`pl_id` = '$pl_id'");
mysql_query($query);
$i++; }
echo mysql_close();
Remove comma before WHERE
mysql_query("UPDATE 'players'
SET block = '$block'
WHERE players.pl_id = '$pl_id'");
You have a } to less, so the PHP code doesn't run.
You do a while loop and a foreach loop, but you are only closing the for loop.
And of course you don't need a , before the WHERE statement