PHP Getting Id Of Submit Button in Next Page - php

if ($result->num_rows > 0)
{
while($row = $result->fetch_assoc())
{
echo "<form name=frm action=edit.php method=POST";
echo "<tr>";
echo "<td>" . $row["Sl_no"]. "</td>";
echo "<td> " . $row["nam"]. "</td>";
echo "<td> " . $row["age"]. " </td>";
echo "<td> " . $row["dob"]. "</td>";
echo "<td> " . $row["gender"]. " </td>";
echo "<td> " . $row["married"]. " </td>";
echo "<td> " . $row["temp_addr"]. " </td>";
echo "<td> " . $row["fix_addrs"]. " </td>";
echo "<td> " . $row["email"]. " </td>";
echo "<td> " . $row["phone"]. " </td>";
echo "<td> " . $row["mother_tounge"]. " </td>";
echo "<td> " . $row["nationality"]. " </td>";
echo "<td> " . $row["clg"]. " </td>";
echo "<td> " . $row["sem"]. " </td>";
echo "<td> " . $row["grad"]. " </td>";
echo "<td> " . $row["qual"]. " </td>";
echo "<td> " . $row["branch"]. " </td>";
echo "<td> <input type=submit name =" .$row["Sl_no"]. " value =Edit ><input type=button id=d".$row["Sl_no"]." value=Delete />";
echo "</tr>";
echo"</form>";
}
I am using this code
and in each field there will be a random named submit will generate.
I want to transfer the name of the button to next page or pass the value of first tag help me.

On the next page you can use $_POST['key']; or $_GET['key']. That will give you access to your value from the key.

You can not post submit button value by using name attribute. just add one hidden field with this form. like
echo "<td> <input type=hidden value=" .$row["Sl_no"]. " name=submit_btn_val >"

You need to pass the value from database to the submit button value, instead of name if you want to get the value on POST, because on post you're getting the name as key and value from input as values, like this:
<input type=submit name="name_here" value ="<?php echo $row['Sl_no']; ?>" >

Related

How to post <input> value to database

My <input> values does not get post to my database when I click the submit button. I am unable to find the error. Please help.
<?php
echo "<form action=Display.php method=post>";
echo "<td>" . $fiberexcel['Engineer9'] ." </td>";
echo "<td>" . $fiberexcel['AM10'] ." </td>";
echo "<td>" . "<input type=date name=A12 value=" . $fiberexcel['Quotation11'] ." </td>";
$A6 = date("Y-m-d");
$ExpectDateQuotation12 = strtotime($ReqDate4."+ 10 weekday");
echo "<td>" . date("Y-m-d",$ExpectDateQuotation12) . "</td>";
$ExpectDateQuotation12 = date("Y-m-d",$ExpectDateQuotation12);
$UpdateQuery = "UPDATE `fiberexcel` SET `ExpectDateQuotation12` =
'$ExpectDateQuotation12' WHERE `fiberexcel`.`SiteID0` = '$A1';";
mysqli_query($conn, $UpdateQuery);
echo "<td>" . "<input type=date name=A14 value=" . $fiberexcel['ApprovalJFSRequest13'] ." </td>";
?>
echo "<td>" . "<input type=hidden name=hidden value=" .$fiberexcel['SiteID0'] ." </td>";
//echo "</tr>";
echo "<td>" . "<input type=submit name=update value=update>". "</td>";
echo "</form>";
This is my update button post function:
if(isset($_POST['update'])){
$UpdateQuery = "UPDATE `fiber`.`fiberexcel` SET Quotation11='$_POST[A12]' , ApprovalJFSRequest13='$_POST[A14]' WHERE `fiberexcel`.`SiteID0`='$_POST[hidden]'";
mysqli_query($conn, $UpdateQuery);
};
Could you please check your HTML output in browser.
I have noticed some issues in your code
-you have missed quotes for every attribute values
ex:
echo "";
wolud be like
echo "";
also there is an error in your HTML code,
ex:
echo "" . "";
here your have missed to close the input tag,
your code wold be like below,
<?php
echo "<form action='Display.php' method='post'>";
echo "<td>" . $fiberexcel['Engineer9'] ." </td>";
echo "<td>" . $fiberexcel['AM10'] ." </td>";
echo "<td>" . "<input type='date' name='A12' value='" . $fiberexcel['Quotation11'] ."'></td>";
$A6 = date("Y-m-d");
$ExpectDateQuotation12 = strtotime($ReqDate4."+ 10 weekday");
echo "<td>" . date("Y-m-d",$ExpectDateQuotation12) . "</td>";
$ExpectDateQuotation12 = date("Y-m-d",$ExpectDateQuotation12);
$UpdateQuery = "UPDATE `fiberexcel` SET `ExpectDateQuotation12` =
'$ExpectDateQuotation12' WHERE `fiberexcel`.`SiteID0` = '$A1';";
mysqli_query($conn, $UpdateQuery);
echo "<td>" . "<input type='date' name='A14' value='" . $fiberexcel['ApprovalJFSRequest13'] ."'> </td>";
echo "<td>" . "<input type='hidden' name='hidden' value='" .$fiberexcel['SiteID0'] ."'> </td>";
//echo "</tr>";
echo "<td>" . "<input type='submit' name='update' value='update'>". "</td>";
echo "</form>";
?>
There is some syntax error in your code. Please check the php close tag and input close tag. Try this..
<?php
echo "<form action='Display.php' method='post'>";
echo "<td>" . $fiberexcel['Engineer9'] ." </td>";
echo "<td>" . $fiberexcel['AM10'] ." </td>";
echo "<td><input type='date' name='A12' value=" . $fiberexcel['Quotation11'] ."/></td>";
$A6 = date("Y-m-d");
$ExpectDateQuotation12 = strtotime($ReqDate4."+ 10 weekday");
echo "<td>" . date("Y-m-d",$ExpectDateQuotation12) . "</td>";
$ExpectDateQuotation12 = date("Y-m-d",$ExpectDateQuotation12);
$UpdateQuery = "UPDATE `fiberexcel` SET `ExpectDateQuotation12` =
'$ExpectDateQuotation12' WHERE `fiberexcel`.`SiteID0` = '$A1';";
mysqli_query($conn, $UpdateQuery);
echo "<td><input type='date' name='A14' value=" . $fiberexcel['ApprovalJFSRequest13'] ." /></td>";
echo "<td><input type='hidden' name='hidden' value=" .$fiberexcel['SiteID0'] ." /></td>";
//echo "</tr>";
echo "<td><input type='submit' name='update' value='update'></td>";
echo "</form>";
?>

How can I make my html code output the data in a valid way?

I'm trying to output the contents of my MySQL user table, i have the code set up in a way in which administrators can add, edit and delete rows within the table. After looking into the issue a bit more however I found that the html code is outputting the values in an awkward way, as it's only counting the first word in the column as a valid value so it ignores the other words. How can I go about fixing this?
For example say i'm trying to output an album called Skulls & Bones the output on the table will just be Skulls.
Admin_Album_Page.php
<!DOCTYPE HTML>
<html>
<head>
<title>View Album Table</title>
</head>
<body>
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
session_start();
require_once 'ConnectorCode.php';
//mysql_query command is used to select data from Albums table
$result = mysqli_query($conn, "SELECT * FROM tbl_Albums");
//Echos setting established onto the main table
echo "<table border='1'>";
echo "<tr> <th>Album ID</th> <th>Album Name</th> <th>Number Of Tracks</th>
<th> Genre </th> <th>Track ID</th> </tr>";
//Results are looped and then displayed in tables while($row = mysqli_fetch_array($result)) {
echo "<form action=Admin_Album_Page.php method=post>";
echo "<tr>";
echo "<td>" . "<input type=number name=albumid value=" . $row['Album_id'] . " </td>";
echo "<td>" . "<input type=text name=albumname value=" . $row['Album_Name'] . " </td>";
echo "<td>" . "<input type=number name=numberoftracks value=" . $row['Number_Of_Tracks'] . " </td>";
echo "<td>" . "<input type=text name=genre value=" . $row['Genre'] . " </td>" ;
echo "<td>" . "<input type=number name=artistid value=" . $row['Artist_id'] . " </td>";
echo "<td>" . "<input type=hidden name=hidden value=" . $row['Album_id'] . " </td>";
echo "<td>" . "<input type=submit name=update value=Update" . " </td>";
echo "<td>" . "<input type=submit name=delete value=Delete" . " </td>";
echo "</form>";
}
echo "<form action=Admin_Artist_Page.php method=post>";
echo "<tr>";
echo "<td></td>";
echo "<td><input type=text name=ualbumname></td>";
echo "<td><input type=text name=unumberoftracks></td>";
echo "<td><input type=text name=ugenre></td>";
echo "<td><input type=number name=uartistid></td>";
echo "<td>" . "<input type=submit name=add value=Add" ." </td>";
echo "</form>";
echo "</table>";
//Connection is closed
mysqli_close($conn);
?>
<p>Return to main page</p>
</body>
</html>
The problem is in your HTML rendering.
You need to change your code to this:
echo "<form action=\"Admin_Album_Page.php\" method=\"post\">";
echo "<tr>";
echo "<td>" . "<input type=\"number\" name=\"albumid\" value=\"" . $row['Album_id'] . "\" </td>";
echo "<td>" . "<input type=\"text\" name=\"albumname\" value=\"" . $row['Album_Name'] . "\" </td>";
echo "<td>" . "<input type=\"number\" name=\"numberoftracks\" value=\"" . $row['Number_Of_Tracks'] . "\" </td>";
echo "<td>" . "<input type=\"text\" name=genre value=" . $row['Genre'] . " </td>" ;
echo "<td>" . "<input type=\"number\" name=\"artistid\" value=\"" . $row['Artist_id'] . "\" </td>";
echo "<td>" . "<input type=\"hidden\" name=\"hidden\" value=\"" . $row['Album_id'] . "\" </td>";
echo "<td>" . "<input type=\"submit\" name=\"update\" value=\"Update" . "\" </td>";
echo "<td>" . "<input type=\"submit\" name=\"delete\" value=\"Delete" . "\" </td>";
echo "</form>"
echo "<form action=\"Admin_Artist_Page.php\" method=\"post\">";
echo "<tr>";
echo "<td></td>";
echo "<td><input type=\"text\" name=\"ualbumname\"></td>";
echo "<td><input type=\"text\" name=\"unumberoftracks\"></td>";
echo "<td><input type=\"text\" name=\"ugenre\"></td>";
echo "<td><input type=\"number\" name=\"uartistid\"></td>";
echo "<td>" . "<input type=\"submit\" name=\"add\" value=\"Add" ."\" </td>";
echo "</form>";
echo "</table>";
Explaining:
HTML works the following way:
<tag attribute="value"></tag>
You can't simply call it by
<tag attribute=value></tag>
In the code that I've showed you, you want to make sure that the " symbol is part of the string, so you escape it using \

cant read the value after space?

while($record= mysqli_fetch_array($myData)){
echo "<form action=cpoarea1update.php method=post>";
echo "<tr>";
echo "<td><label>". $record['CODE'] . " </td>";
echo "<td>". "<input type=text name=dt value =" . $record['DOCUMENT_TITLE']. " </td>";
echo "<td>". "<input type=text name=rv value=" . $record['REVISION']. " </td>";
echo "<td>". "<input type=date name=dateis value=" . $record['DATE_OF_ISSUE']. " </td>";
echo "<td>". "<input type=hidden name=hidden value=" . $record['CODE']. " </td>";
echo "<td>". "<input type=submit name=update value=update " . " </td>";
echo "</tr>";
echo "</form>";
}
echo "</table>";
For example if the data in MySQL is "Hello my name is Maxim", I only see "Hello" in the textbox. What's the problem? Thanks in advance
You have to wrap <input> attribute(s) by quotes. Also, you need to close tags!
echo "<td><input type=text name=dt value =\"" . $record['DOCUMENT_TITLE']. "\"> </td>";
^ ^ ^
Your original code output this not valid HTML:
<td><input type=text name=dt value =document title </td>
Best practice is to always use quotes:
<td><input type="text" name="dt" value="document title"></td>

Whole text won't show in a table, PHP

I'm trying to is have have table to display my database with an update, delete, and add functions.
My problem is when I view it. It doesn't show the full text in the "Description" column and also in "Developer". Also in the "SecondDirectory" column it shows </td instead of just blank if its blank.
<html>
<head>
</head>
<body>
<?php
$con = mysql_connect("$$$$","$$$$","$$$$");
if (!$con){
die("Can not connect: " . mysql_error());
}
mysql_select_db("mydb",$con);
if(isset($_POST['update'])){
$UpdateQuery = "UPDATE table SE ID='$_POST[topic]',Filename='$_POST[filename]', Description='$_POST[description]', TopDirectory='$_POST[topdirectory]', SecondDirectory='$_POST[seconddirectory]', Developer='$_POST[developer]', Date='$_POST[date]' WHERE ID='$_POST[hidden]'";
mysql_query($UpdateQuery, $con);
};
if(isset($_POST['delete'])){
$DeleteQuery = "DELETE FROM table WHERE ID='$_POST[hidden]'";
mysql_query($DeleteQuery, $con);
};
if(isset($_POST['add'])){
$AddQuery = "INSERT INTO table (ID,Filename,Description,TopDirectory,SecondDirectory,Developer,Date) VALUES ('$_POST[uid]','$_POST[ufilename]','$_POST[udescription]','$_POST[utopdirectory]','$_POST[useconddirectory]','$_POST[udeveloper]','$_POST[udate]')";
mysql_query($AddQuery, $con);
};
$sql = "SELECT * FROM table";
$myData = mysql_query($sql,$con);
echo "<table border=1>
<tr>
<th>ID</th>
<th>Filename</th>
<th>Description</th>
<th>TopDirectory</th>
<th>SecondDirectory</th>
<th>Developer</th>
<th>Date</th>
</tr>";
while($record = mysql_fetch_array($myData)){
echo "<form action=theworks.php method=post>";
echo "<tr>";
echo "<td>" . "<input type=text name=id value=" . $record['ID'] . " </td>";
echo "<td>" . "<input type=text name=filename value=" . $record['Filename'] . " </td>";
echo "<td>" . "<input type=text name=description value=" . $record['Description'] . " </td>";
echo "<td>" . "<input type=text name=topdirectory value=" . $record['TopDirectory'] . " </td>";
echo "<td>" . "<input type=text name=seconddirectory value=" . $record['SecondDirectory'] . " </td>";
echo "<td>" . "<input type=text name=developer value=" . $record['Developer'] . " </td>";
echo "<td>" . "<input type=text name=date value=" . $record['Date'] . " </td>";
echo "<td>" . "<input type=hidden name=hidden value=" . $record['ID'] . " </td>";
echo "<td>" . "<input type=submit name=update value=update" . " </td>";
echo "<td>" . "<input type=submit name=delete value=delete" . " </td>";
echo "</tr>";
echo "</form>";
}
echo "<form action=database.php method=post>";
echo "<tr>";
echo "<td><input type=text name=uid></td>";
echo "<td><input type=text name=ufilename></td>";
echo "<td><input type=text name=udescription><td>";
echo "<td><input type=text name=utopdirectory></td>";
echo "<td><input type=text name=useconddirectory></td>";
echo "<td><input type=text name=udeveloper><td>";
echo "<td><input type=text name=udate></td>";
echo "<td>" . "<input type=submit name=add value=add" . " </td>";
echo "</tr>";
echo "</form>";
echo "</table>";
mysql_close($con);
?>
</body>
</html>
So I figured it out.
echo "<td>" . "<input type=text name=description value=" . $record['Description'] . " </td>";
What I needed to put is a single quotation right after value= and two single quotations in " " so that it also counts the spaces. The code should have been...
echo "<td>" . "<input type=text name=description value='" . $record['Description'] . "'' </td>";

Update is not working in database

Dont mind the security issues, this is just local testing, but when ever i click the update button none of the changes go through on the page or on the query and i get no erros.
<?php
$link = mysqli_connect("localhost", "root", "", "test") or die("could not connect");
if (isset($_POST['update'])) {
$updateQuery = (" UPDATE `test1` SET f_name = '$_POST[f_name]', l_name='$_POST[l_name]', email='$_POST[email]' WHERE id='$_POST[id]'");
mysqli_query($link, $updateQuery);
};
$query = ("SELECT * FROM `test1`");
$result = mysqli_query($link, $query);
echo "<table border=1
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Email</th>
</tr>";
while($row = mysqli_fetch_array($result)) {
echo "<form method=post action=update.php>";
echo "<tr>";
echo "<td>" . "<input type=text name=f_name value=" . $row['f_name'] . " </td>";
echo "<td>" . "<input type=text name=l_name value=" . $row['l_name'] . " </td>";
echo "<td>" . "<input type=text name=email value=" . $row['email'] . " </td>";
echo "<td>" . "<input type=hidden name=id value=" . $row['id'] . " </td>";
echo "<td>" . "<input type=submit name=submit value=update" . " </td>";
echo "</tr>";
}
?>
change your form to
while($row = mysqli_fetch_array($result)) {
echo "<form method=post action=update.php>";
echo "<input type=hidden name=update>";
echo "<tr>";
echo "<td>" . "<input type=text name=f_name value=" . $row['f_name'] . " </td>";
echo "<td>" . "<input type=text name=l_name value=" . $row['l_name'] . " </td>";
echo "<td>" . "<input type=text name=email value=" . $row['email'] . " </td>";
echo "<td>" . "<input type=hidden name=id value=" . $row['id'] . " </td>";
echo "<td>" . "<input type=submit name=submit value=update" . " </td>";
echo "</tr>";
}
POST keys should be in quotes. Try this instead:
$updateQuery = "UPDATE test1 SET f_name = ".$_POST['f_name'].", l_name=."$_POST['l_name'].", email=".$_POST['email']." WHERE id=".$_POST['id'];
Try this:
$updateQuery = ("UPDATE `test1` SET f_name = '{$_POST['f_name']}', l_name='{$_POST['l_name']}', email='{$_POST['email']}' WHERE id='{$_POST['id']}'");
Also you can try echoing something inside your if (isset($_POST['update'])) { to make sure it is testing true.
Here is your problem:
if (isset($_POST['submit']) && $_POST['submit'] == 'update') {
The name of the submit button is submit not update, the value is update.

Categories