Edit sql data in php page - php

I need to edit my database table using submitted data.
This is the form:
mysql_query("set names 'utf8'");
$query = "SELECT * FROM sec1octa";
$result = mysql_query($query);
?>
<div align="center">
<form method="get" action="edit_data.php">
<table width="104" border="1" class="center1">
<tr>
<th width="94">first</th>
<th width="94">second</th>
<th width="94">status</th>
</tr>
<tr>
<?php
if (mysql_num_rows($result) > 0) {
while ($row = mysql_fetch_array($result)) {
?>
<tr>
<td><input type="text" name="id" value="<?php echo $row ['stu_no']; ?> " size=10></td>
<td><input type="text" name="name" value="<?php echo $row ['stu_name']; ?> " size=10></td>
<td><?php
echo '<select name="status">'; {
echo '<option value="open">'.$row['stu_status'].'</option>';
echo '<option value="close">'.prevent.'</option>';
}
echo '</select>';
?></td>
</tr>
<?php
}
}
?>
</tr>
</table>
<input type="submit" name="submit" value="done" />
</form>
The problem is in the edit_data.php page.
I can't UPDATE.
I use this code but it's not working.
require_once('../Connections/config.php');
$id= $_GET['id'];
$status= $_GET['status'];
$query= mysql_query("UPDATE `goh`.`sec1octa` SET `stu_status` = '$status'
WHERE stu_no='".$id."'") or die (mysql_error ());
if($query){echo $status ."done ";}

The reason you are only getting the last values in your edit_data.php $_GET is because you are not setting the input/select names as arrays.
<input type="text" name="id" value="some_stu_no">
is happening over and over and over and every new one overwrites the previous.
Instead, you should use:
<input type="text" name="id[]" value="some_stu_no">
This will allow you to pass multiple id's in a single form submission.
Your form:
<form method="POST" action="edit_data.php">
....
echo "<tr>";
echo "<th>id</th>";
echo "<th>name</th>";
echo "<th>status</th>";
echo "</tr>";
if(mysql_num_rows($result)>0){
while($row=mysql_fetch_array($result)){
echo "<tr>";
echo "<td><input type=\"text\" name=\"id[]\" value=\"{$row['stu_no']}\" size=\"10\"></td>";
echo "<td>{$row['stu_name']}</td>";
echo "<td>";
echo "<select name=\"status[]\">"; // I don't like your option set up here, but I don't fully understand it either.
echo "<option value=\"open\">{$row['stu_status']}</option>";
echo "<option value=\"close\">.prevent.</option>";
echo "</select>";
echo "</td>";
echo "</tr>";
}
}
....
<input type="submit" value="Submit All">
</form>
edit_data.php
// create a mysqli connection called $db
if(isset($_POST['id'])){
$tally=0;
// build all queries for the batch
foreach($_POST['id'] as $index=>$id){
$queries[]="UPDATE `goh`.`sec1octa` SET `stu_status`='".mysqli_real_escape_string($db,$_POST['status'][$index])."' WHERE `stu_no`='".mysqli_real_escape_string($db,$id)."'";
}
// run all queries
if(mysqli_multi_query($db,implode(';',$queries)){
do{
$tally+=mysqli_affected_rows($db);
} while(mysqli_more_results($db) && mysqli_next_result($db));
}
// assess the outcome
if($error_mess=mysqli_error($db)){
echo "Syntax Error: $error_mess";
}else{
echo "$tally row",($tally!=1?"s":"")," updated";
}
mysqli_close($con);
}

Related

how to display auto increment data form mysql in php

In my university database project I have an auto increment field roll number in my SQL. What I want is that when new admission take place and a student record is inserted it displays all finds on same page including roll number. However despite my best efforts all it returns 0 in roll number.
Here is the code:
<?php
$con = mysqli_connect("localhost", "root", "") or die("conection error");
mysqli_select_db($con, "hamdard university") or die("dbase error");
if (isset($_POST['subbtn'])) {
$r = "SELECT RollNo FROM admission_form";
$result = mysqli_query($con, $r);
if (mysqli_query($con, $r)) {
$last_id = mysqli_insert_id($con);
}
$n = $_POST['txtname'];
$f = $_POST['txtfac'];
$s = $_POST['txtsem'];
$sql = "insert into admission_form(name,faculty,semester)values ('$n','$f','$s')";
mysqli_query($con, $sql);
echo "<table border=1>
<th>RollNo</th>
<th>Name</th>
<th>Faculty</th>
<th>Semester</th>";
echo "<tr>";
echo "<td>";
echo $last_id;
echo "</td>";
echo "<td>";
echo $n;
echo "</td>";
echo "<td>";
echo $f;
echo "</td>";
echo "<td>";
echo $s;
echo "</td>";
echo "<br>";
}
?>
<html>
<head></head>
<body>
<form name="f1" action="" method="POST">
RollNo:
<input type="text" name="txtroll" readonly> Name:
<input type="text" name="txtname"> Faculty:
<input type="text" name="txtfac"> Semester:
<input type="text" name="txtsem">
<input type="submit" value="done" name="subbtn">
</form>
</body>
</html>
You need get $last_id after INSERT query
$sql="insert into admission_form(name,faculty,semester)values ...
mysqli_query($con,$sql);
$last_id = mysqli_insert_id($con);
You have misplaced insert query. Change it as:
<?php
$con=mysqli_connect("localhost","root","")or die("conection error");
mysqli_select_db($con,"hamdard university")or die("dbase error");
if(isset($_POST['subbtn']))
{
$sql="insert into admission_form(name,faculty,semester)values ('$n','$f','$s')";
if (mysqli_query($con, $sql))
{
$last_id = mysqli_insert_id($con);
}
$n=$_POST['txtname'];
$f=$_POST['txtfac'];
$s=$_POST['txtsem'];
$r="SELECT RollNo FROM admission_form";
$result=mysqli_query($con, $r);
echo "<table border=1>
<th>RollNo</th>
<th>Name</th>
<th>Faculty</th>
<th>Semester</th>";
echo "<tr>";
echo "<td>";
echo $last_id;
echo "</td>";
echo "<td>";
echo $n;
echo "</td>";
echo "<td>";
echo $f;
echo "</td>";
echo "<td>";
echo $s;
echo "</td>";
echo "<br>";
}
?>
<html>
<head></head>
<body>
<form name="f1" action="" method="POST">
RollNo:<input type="text" name="txtroll" readonly>
Name:<input type="text" name="txtname">
Faculty:<input type="text" name="txtfac">
Semester:<input type="text" name="txtsem">
<input type="submit" value="done" name="subbtn">
</form>
</body>
</html>
You have some problems in this page.
As stated before, the INSER query must be before the select.
If you do "SELECT RollNo FROM admission_form" you will get ALL existing RollNo in admission_form and not the lastest one. If this is an int with auto increment (that seams to be) you should do "SELECT max(RollNo) FROM admission_form".
You don't need to do the above select, as mysqli_insert_id get the ID inserted in the last query.
You might not see the result in your table because it is on HTML head, but it should be in the body of the page.
The table you create is not "closed".
You should be good to go with the code bellow.
<HTML>
<HEAD></HEAD>
<BODY>
<?php
$con=mysqli_connect("localhost","root","")or die("conection error");
mysqli_select_db($con,"hamdard university")or die("dbase error");
if(isset($_POST['subbtn']))
{
$n=$_POST['txtname'];
$f=$_POST['txtfac'];
$s=$_POST['txtsem'];
$sql="insert into admission_form(name,faculty,semester) values ('$n','$f','$s')";
if (mysqli_query($con, $r))
{
$last_id = mysqli_insert_id($con);
}
echo "<table border=\"1\"><th>RollNo</th> <th>Name</th> <th>Faculty</th> <th>Semester</th>";
echo "<tr>";
echo "<td>";
echo $last_id;
echo "</td>";
echo "<td>";
echo $n;
echo "</td>";
echo "<td>";
echo $f;
echo "</td>";
echo "<td>";
echo $s;
echo "</td>";
echo "</tr></table>";
}
?>
<form name="f1" action="" method="POST">
RollNo:<input type="text" name="txtroll" readonly>
Name:<input type="text" name="txtname">
Faculty:<input type="text" name="txtfac">
Semester:<input type="text" name="txtsem">
<input type="submit" value="done" name="subbtn">
</form>
</body>
</html>
Try this:
$mysqli = new mysqli(SQLI_SERVER, MYSQLI_USER, MYSQLI_PWD, MYSQLI_DBNAME);
if ($result = $mysqli->query("INSERT INTO admission_form(name, facility,semester) VALUES..) {
echo 'The ID is: '.$mysqli->insert_id;
}

Stop Inserting Blank data in Refreshing browser

I'm new in PHP. some one please help to stop inserting blank data when refreshing the browser. I'm trying to insert data in SQL database from a HTML form & showing them in the same page in a table.
here is my from & PHP code
Thanks in advance....
</head>
<body>
<?php
$sub = mysql_connect ("localhost", "root","");
if (!$sub)
{
die('Could Not Connect:'.mysql_erro());
}
mysql_select_db("test", $sub);
$sql="INSERT INTO te(Dat,Sadi,Jam,Washi) VALUES('$_POST[Dat]','$_POST[Sadi]','$_POST[Jam]','$_POST[Washi]')";
if (!mysql_query($sql, $sub))
{
die('Error:'.mysql_error());
}
echo 'One Record added';
$result=mysql_query("SELECT * FROM te");
echo "<table border='1'>;
<tr>
<th>Date</th>
<th>Sadi</th>
<th>Jam</th>
<th>Washi</th>
</tr>";
while ($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" .$row['Dat']."</td>";
echo "<td>" .$row['Sadi']."</td>";
echo "<td>" .$row['Jam']."</td>";
echo "<td>" .$row['Washi']."</td>";
echo "</tr>";
}
echo "</table>";
Mysql_close($sub);
?>
</body>
<form action="index.php" method="post">
<input type="text" name="Dat" placeholder="Date, DD-MM-YY">
<input type="text" name="Sadi" placeholder="Sadi">
<input type="text" name="Jam" placeholder="Jam">
<input type="text" name="Washi" placeholder="Washi">
<input type="submit" name="sub">
</form>
Just verify if the $_POST is not empty
if(!empty($_POST))
{
$sql="INSERT INTO te(Dat,Sadi,Jam,Washi) VALUES('$_POST[Dat]','$_POST[Sadi]','$_POST[Jam]','$_POST[Washi]')";
if (!mysql_query($sql, $sub))
{
die('Error:'.mysql_error());
}
}

PHP Insert table into database

I have a database table that I created from a .csv file. I used php to edit the content to match a new table on my database. I now need to INSERT that new table into the new database. I added form tags around the table and a submit button, and when I submit it I only get 1 or 2 random rows of the table. I am guessing that I might need a while or a for loop, but I cant figure out how to write it. Any assistance would be appreciated.
This is the code to post to the db
if(isset($_POST['submit'])){
$mr = $_POST['mr'];
$mrs = $_POST['mrs'];
$last_name = $_POST['last_name'];
$marital_status = $_POST['marital_status'];
$query = "INSERT INTO import_template(first_name, last_name, marital_status) VALUES('{$mr}', '{$last_name}', '{$marital_status}')";
$result = mysqli_query($connection, $query);
if(!$result){
die("QUERY FAILED " . mysqli_error($connection));
}
if(!$connection){
echo "We are NOT connected";
}
$query = "INSERT INTO import_template(first_name, last_name, marital_status) VALUES('{$mrs}', '{$last_name}', '{$marital_status}')";
$result = mysqli_query($connection, $query);
if(!$result){
die("QUERY FAILED " . mysqli_error($connection));
}
if(!$connection){
echo "We are NOT connected";
}
}
This is what the dynamic table looks like I only added the input to the first few to test, I haven't done the rest yet.
echo "<tr>";
echo "<td><input type='text' class='form-control' name='mr' value='$mr'></td>";
echo "<td><input type='text' class='form-control' name='last_name' value='$last_name'></td>";
echo "<td><input type='text' class='form-control' name='marital_status' value='$marital_status'></td>";
echo "<td>{$birthday}</td>";
echo "<td>Adult</td>";
echo "<td>{$anniversary}</td>";
echo "<td>{$mr_cell_phone}</td>";
echo "<td>{$home_phone}</td>";
echo "<td>{$mr_email}</td>";
echo "<td>{$street}</td>";
echo "<td>{$city}</td>";
echo "<td>{$state}</td>";
echo "<td>{$zip}</td>";
echo "<td></td>";
echo "</tr>";
echo "<tr>";
echo "<td><input type='text' class='form-control' name='mrs' value='$mrs'></td>";
echo "<td><input type='text' class='form-control' name='last_name' value='$last_name'></td>";
echo "<td><input type='text' class='form-control' name='marital_status' value='$marital_status'></td>";
echo "<td></td>";
echo "<td>Adult</td>";
echo "<td>{$anniversary}</td>";
echo "<td>{$mrs_cell_phone}</td>";
echo "<td></td>";
echo "<td>{$mrs_email}</td>";
echo "<td></td>";
echo "<td></td>";
echo "<td></td>";
echo "<td></td>";
echo "<td></td>";
echo "</tr>";
You can do like this. I have taken three fields such as task,agent and note.You can add as per your table rows:
main.php
<form id="first" action="insert_values.php" method="post">
<table>
<tr>
<td>Task</td>
<td>
<input type="text" name="task">
</td>
</tr>
<tr>
<td>Agent</td>
<td>
<input type="text" name="agent">
</td>
</tr>
<tr>
<td>Note</td>
<td><input type="text" name="Note"></td>
</tr>
</table>
<input type="submit" value="save" name="save">
</form>
insert_values.php
<?php
error_reporting( error_reporting() & ~E_NOTICE );
session_start();
include 'connect.php';
$task=$_POST['task'];
$agent=$_POST['agent'];
$Note=$_POST['Note'];
$result=mysql_query("insert into table_name(task,agent,t_note) values('$task','$agent','$Note')");
if($result)
{
$msg="Insert Successfully!!";
echo "<script>alert('Insert Successfully');document.location='main.php'</script>";
?>
<?php
}
else
{
$errmsg=" Insert not success!!";
echo "<script>alert('Insert Not Successfully');document.location='main.php'</script>";
}
?>

Passing php variable from one form to another page with hidden value

After passing a php variable(Tno) from one page to another page using button with hidden value, with that variable(Tno) tried to retrieve the data from the database, but I am not able to retrieve with that variable i.e. Tno. I had attached form1.php and retrieve.php.
form1.php
<?php
session_start();
?>
<?php
if( isset ($_SESSION["Email"])) {
$con=mysql_connect("localhost","admin","password");
// Check connection
if (!$con)
{
//echo "i am not ale to connect.<br>";
die('Could not connect: ' . mysql_error());
}
else{
//echo " i am able to connect.<br>";
}
mysql_select_db("maintable", $con);
$Email = mysql_real_escape_string($_SESSION['Email']);
$sql1="select * from customer where Email=('$Email')";
$result1=mysql_query($sql1);
while($row = mysql_fetch_array($result1)){
$var1 = $row['Identity'];
}
//echo" Email of the customer = $Email.<br>";
//echo "identity of the customer = $var1.<br>";
$sql2= "SELECT * FROM `suptest` WHERE Tno LIKE '$var1%'";
$result2=mysql_query($sql2); ?>
<table border=0 width=50%>
<tr>
<th>Ticket Ref. No.</th> <th>Subject</th><th>Status</th></tr>
<?php
while($row = mysql_fetch_array($result2)){
$ticketno= $row['Tno'];
?>
<form action="retrieve.php" method="post">
<tr><td><?php echo $row['Tno']; ?><input type="text" value="<?php echo $row['Tno']; ? >" name="Tno" /></td>
<td><?php echo $row['sub']; ?></td>
<td><?php echo $row['status']; ?></td>
<td><input type="Submit" value="Update" /></td></tr>
</form>
<?php
} ?>
</table>
<?php
mysql_close($con);
}
?>
<?php
session_start();
?>
<html><head></head>
<body>
<?php
$con=mysql_connect("localhost","admin","password");
//echo "Check connection";
if (!$con)
{
//echo "I am not ale to connect.<br>";
die('Could not connect: ' . mysql_error());
}
else{
//echo "I am able to connect.<br>";
}
mysql_select_db("maintable", $con);
$Tno = mysql_real_escape_string($_POST['Tno']);
//echo "my tno is $Tno.<br>";
$Email = mysql_real_escape_string($_SESSION['Email']);
echo $Email;
$sql = "select * from `active1active1` where Tno = ('$Tno')";
if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
$result=mysql_query($sql);
echo $result;
echo mysql_num_rows($result);
echo "entering while loop";
while($row=mysql_fetch_array($result))
{
echo "entered while loop";
echo $row['Tno'] . "<br />";
echo $row['Email'] . "<br />";
echo $row['pdesc'] . "<br />";
echo $row['Activity'] . "<br />";
}
?>
</body></html>
im going to take a stab here:
this line
<tr><td><?php echo $row['Tno']; ?><input type="text" value="<?php echo $row['Tno']; ? >" name="Tno" /></td>
should be this?
<tr><td><?php echo $row['Tno']; ?><input type="hidden" name='Tno' value="<?php echo $row['Tno']; ? >" name="Tno" /></td>
note i added type=hidden and a name for the input
try:
$sql = "select * from `active1active1` where Tno = '" . $Tno . "'";
value="<?php echo $row['Tno']; ? >
There should not be any space after ?
change to ?>
<form action="retreive.php" method="post">
<tr>
<td>
<input type="hidden" value="<?php echo $row['Tno']; ?>" name="Tno" />
</td>
<td>
<input type="Submit" value="Update" />
</td>
</tr>
</form>
retrie`
try this simply:
$Tno = mysql_real_escape_string($_POST['Tno']);
echo $sql = "select * from `active1active1` where Tno = '$Tno'";
$result=mysql_query($sql);
while ($row = mysql_fetch_array($result))
{
print_r($row);
}

Need to get values in textbox from database on button click in PHP

I need to get values of database in text boxes on button click using PHP and Javascript. For instance, I get values in an HTML table from a database table. I need to get the respective values in the text boxes when the user clicks on the add0 button.
Here is my code:
<form method="post" action="">
<input type="text" name="tb1" />
<input type="text" name="tb2" />
<input type="submit" name="btn" value="Find" />
</form>
<?php
include "conn.php";
$show = "SELECT * FROM data";
$rs = mysql_query($show) or die(mysql_error());
$add_to_textbox = "<input type='button' name='btn' value='add0' />";
#****results in Grid****
echo "<table width='360px' border='1' cellpadding='2'>";
while($row = mysql_fetch_array($rs)) {
echo "<tr>";
echo "<td width='130px'>$row[Name]</td>";
echo "<td width='230px'><a href = '$row[Link]'>$row[Link]</a></td>";
echo "<td width='130px'>$add_to_textbox</td>";
echo "</tr>";
}
echo "</table>";
#**********************
mysql_free_result($rs);
?>
I need further code on button click.
imho you can use Inline edit using Ajax in Jquery
Here is it's demo
It will let you edit your displayed contents in the table itself..
Update:
<form method="post" action="">
<input type="text" name="tb1" id="tb1" />
<input type="text" name="tb2" id ="tb2" />
<input type="submit" name="btn" value="Find" />
</form>
<?php
include "conn.php";
$show = "SELECT * FROM data";
$rs = mysql_query($show) or die(mysql_error());
$add_to_textbox = "<input type='button' name='btn' value='add0' />";
#****results in Grid****
echo "<table width='360px' border='1' cellpadding='2'>";
$rowID=1;
while($row = mysql_fetch_array($rs)) {
echo "<tr>";
echo "<td width='130px' id='name".$rowID."'>$row[Name]</td>";
echo "<td width='230px' id='link".$rowID."'><a href = '$row[Link]'>$row[Link]</a></td>";
echo "<td width='130px' onclick='txtValDisp($rowID);'>$add_to_textbox</td>";
echo "</tr>";
$rowID++;
}
echo "</table>";
#**********************
mysql_free_result($rs);
?>
<script type="text/javascript">
function txtValDisp(rowID){
var linkVal = document.getElementById('link'+rowID+'').innerHTML.replace(/<\/?[^>]+(>|$)/g, "\n");
document.getElementById("tb1").value = document.getElementById('name'+rowID+'').innerHTML;
document.getElementById("tb2").value = linkVal;
}
</script>
Recreate your form with default values taken from the database.
<form method="post" action="">
<input type="text" name="tb1" />
<input type="text" name="tb2" />
<input type="submit" name="btn" value="Find" />
</form>
<?php
include "conn.php";
$show = "SELECT * FROM data";
$rs = mysql_query($show) or die(mysql_error());
$add_to_textbox = "<input type='button' name='btn' value='add0' />";
#****results in Grid****
echo "<table width='360px' border='1' cellpadding='2'>";
while($row = mysql_fetch_array($rs)) {
echo "<tr>";
echo "<td><input name ='INSERT_HERE' type=text value='"$row[Name]"'></td>";
echo "</tr>";
}
echo "</table>";
#**********************
mysql_free_result($rs);
?>
You just need to change the name of the object based on whatever counter of something...

Categories