UPDATE record DB from form PHP - php

I have a page "elenco.php" where i show all DB record in a table and in each row at the end i have a button "edit" that submit to the page "edit.php" the "ID" of the row that the user would like to edit .
elenco.php
<?php
include '/var/www/phpMyEdit/open_dati.php';
$query = "SELECT * FROM scadenziario";
$result= mysql_query($query);
echo "<h2>Elenco Scadenziario</h2>";
echo "<table border>";
echo "<tr><td>ID</td><td>Aspetto Generale</td><td>Descrizione</td><td>Ragione Sociale</td><td>Numero Civico</td><td>Validita</td><td>Data</td><td>Preavviso</td><td>Scadenza</td><td>Prescrizioni</td><td>Frequenza</td><td>Data Controllo</td><td>Prossimo Controllo</td><td>Note</td>";
while($row = mysql_fetch_array($result)){
echo "<tr><td>" . $row[ID] .
"</td><td>". $row[Aspetto_Generale].
"</td><td>". $row[Descrizione].
"</td><td>". $row[Ragione_Sociale].
"</td><td>". $row[Num_Civico].
"</td><td>". $row[Validita].
"</td><td>". $row[Data].
"</td><td>". $row[Preavviso].
"</td><td>". $row[Scadenza].
"</td><td>". $row[Prescrizioni].
"</td><td>". $row[Frequenza].
"</td><td>". $row[Data_Controllo].
"</td><td>". $row[Prox_Controllo].
"</td><td>". $row[Note].
"</td><td><form action='edit.php' method='POST'><input type='hidden' name='tempID' value='".$row['ID']."'/><input type='submit' name='submit-btn' value='Edit' /><form></td></tr>";
}
echo "</table>";
mysql_close();
?>
edit.php
<?echo'
<html>
<head>
<link rel="stylesheet" type="text/css" href="form.css">
</head>';
include '/var/www/phpMyEdit/open_dati.php';
$temp = $_POST['tempID'];
$query = "SELECT * FROM scadenziario WHERE ID = '$temp' ";
$result= mysql_query($query);
while($row = mysql_fetch_array($result)){
$aspettogen = $row['Aspetto_Generale'];
$desc = $row['Descrizione'];
$ragsoc = $row['Ragione_Sociale'];
$numcivico = $row['Num_Civico'];
$validita = $row['Valdita'];
$odierna = $row['Data'];
$preavviso = $row['Preavviso'];
$scadenza = $row['Scadenza'];
$presc = $row['Prescrizioni'];
$freq = $row['Frequenza'];
$datacontr = $row['Data_Controllo'];
$proxcontr = $row['Prox_Controllo'];
$note = $row['Note'];
}?>
<? echo'
<body>
<div class="container">
<form id="contact" method="POST">'?>
<? echo'
<img src="logo.jpg">
<fieldset>
<input name="aspettogen2" type="text" value="'.$aspettogen.'">
</fieldset>
<fieldset>
<input name="desc" type="text" maxlength="255" value="'.$desc.'">
</fieldset>
<fieldset>
<input name="ragsoc" type="text" maxlength="100">
</fieldset>
<fieldset>
<input name="numcivico" type="text" maxlength="20">
</fieldset>
<fieldset>
<input name="validita" type="text">
</fieldset>
<fieldset>
<input name="odierna" type="data">
</fieldset>
<fieldset>
<input name="preavviso" type="text">
</fieldset>
<fieldset>
<input name="scadenza" type="text">
</fieldset>
<fieldset>
<input name="presc" type="text" maxlength="255">
</fieldset>
<fieldset>
<input name="freq" type="number">
</fieldset>
<fieldset>
<input name="datacontr" type="text">
</fieldset>
<fieldset>
<input name="proxcontr" type="text">
</fieldset>
<fieldset>
<input name="note" type="text">
</fieldset>
</form>
</div>
</body>
</html>';
mysql_close();
?>
but i cant understand why in the page edit.php the id value is always the last record of the table DB.
for example my table has 35 record, if i click the edit button of the row 10 in the page "elenco.php", in the page edit.php i receive always ID = 35 ( the last record of the DB).
Can u help me guys?

Replace your mysql* functions for mysqli* functions in elenco.php and edit.php.
then check everything inside the post array, and make sure you're getting all the data with
var_dump($_POST);
and this code:
$query = "SELECT * FROM scadenziario WHERE ID = '$temp' ";
i assume your id is an integer type, so you don't have to use ' ', try:
$query = "SELECT * FROM scadenziario WHERE ID = $temp ";
hope this can help.

Related

Using GET and Post method in the same page

As you can see I get the id from get method, the problem is when I using the $id to update the record it doesnt work but if I replace the $id with a number it works just fine, for example ($query = "UPDATE article SET title='$title_up', utitle='$utitle_up', text='$text_up', image='$image_up' WHERE id=2";)
PHP code:
$id = $_GET['id'];
$sql="SELECT * FROM article WHERE id = '$id'";
$result=mysqli_query($conn,$sql);
while($row = mysqli_fetch_array($result)){
$image = $row['image'];
$title = $row['title'];
$utitle = $row['utitle'];
$text = $row['text'];
}
if(isset($_POST['update'])){
$title_up = mysqli_real_escape_string($conn, $_REQUEST['title']);
$utitle_up = mysqli_real_escape_string($conn, $_REQUEST['utitle']);
$image_up = mysqli_real_escape_string($conn, $_REQUEST['image']);
$text_up = mysqli_real_escape_string($conn, $_REQUEST['text']);
$query = "UPDATE article SET title='$title_up', utitle='$utitle_up', text='$text_up', image='$image_up' WHERE id='$id'";
mysqli_query($conn, $query);
if ($conn->query($query) === TRUE) {
echo "Record updated successfully";
} else {
echo "Error updating record: " . $conn->error;
}
}
HTML form code:
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="POST">
<label for='title'>Title: </label> <br>
<input type='text' id='title' name='title' value="<?php echo$title;?>"/> <br>
<label for='utitle'>UTitle: </label> <br>
<input type='text' id='utitle' name='utitle' value="<?php echo$utitle;?>"/> <br>
<label for='text'>Text: </label> <br>
<input type='text' id='text' name='text' value="<?php echo$text;?>"/> <br>
<input type='text' id='image' name='image' value="<?php echo$image;?>" /> <br>
<input type="submit" name="update" value="Update" />
</form>
Replace this line :
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="POST">
By this :
<form action="<?php echo htmlspecialchars($_SERVER["REQUEST_URI"]);?>" method="POST">
EDIT :
$_SERVER["PHP_SELF"] // return just file
$_SERVER["REQUEST_URI"] // return file and GET parameters

Passing multiple variable through a form

I have the following code, the variable 'name' and 'project' need to go to session variables on submit, in this case the submit happens through an 'onchange'event. The 'name' variable is POSTed but not the 'project' variable. Any ideas where my issue is?
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<?php
$query = $conn->prepare("SELECT name, project FROM models.models where models.active = 'yes'");
$query->execute();
while($r = $query->fetch(PDO::FETCH_OBJ)){
echo "
<div class='swiper-slide'>
<input type='image' src='models/thumbs/",$r->name,".jpg' id='name' name='name' value='",$r->name,"' onchange='this.form.submit();'/>
<input type='hidden' value='",$r->project,"' name='project' />
</div>
";
}
?>
</form>
<?php
$query = $conn->prepare("SELECT name, project FROM models.models where models.active = 'yes'");
$query->execute();
while($r = $query->fetch(PDO::FETCH_OBJ)){
echo "
<div class='swiper-slide'>
<input type='image' src='models/thumbs/",$r->name,".jpg' id='name' name='name[]' value='",$r->name,"' onchange='this.form.submit();'/>
<input type='hidden' value='",$r->project,"' name='project' />
</div>
";
}
?>
How to access:
$names = $_POST['name'];
for($i=0;$i<count($names);$i++)
{
echo $names[$i]."<br>";
}

Display records from database

I'm trying to display some information stored in MySQL comments table to an input but I'm having issues with that. Input named enterComment inserts data to my DB and I want it to redirect back to showComment input.
HTML:
form action='takedata.php' method='POST'>
<input type='text' id='enterComment' name='enterComment' width='400px' placeholder='Write a comment...'>
<input type='submit' id='combuton' name='comButon' value='Comment!'>
<input type='text' id='showComment' name='showComment'>
</form>
PHP:
<?php include "mysql.php"; ?>
<?php
if (isset($_POST['comButon'])){
$enterComment = strip_tags($_POST['enterComment']);
if($enterComment){
$addComment = mysql_query("INSERT INTO comments(comment) VALUES('$enterComment')");
if($addComment==1)
//INSERT INTO showComment input;
}
}
?>
try this, and use mysqli instead of mysql
include "dbconnect.php";
if (isset($_POST['comButon'])){
$enterComment = strip_tags($_POST['enterComment']);
if($enterComment){
$addComment = mysqli_query($conn, "INSERT INTO comments(comment) VALUES('$enterComment')");
if($addComment) {
$sql = "select comment from comments order by id desc limit 1";
$result = mysqli_query($conn, $sql);
while($row = $result->fetch_assoc()) { ?>
<input type="text" value="<?php echo $row['comment']; ?>">
<?php }
}
}
}
your form
<form action='' method='POST'>
<input type='text' id='enterComment' name='enterComment' width='400px' placeholder='Write a comment...'>
<input type='submit' id='combuton' name='comButon' value='Comment!'>
<?php if(!isset($_POST['comButon'])) { ?>
<input type="text" value="">
<?php } ?>
</form>

Using SELECT from WHILE as value in html input PHP SQL

I am a complete beginner, so please bear with me. This is just a test project I am putting together to try to teach myself some of the basics.
I know that a lot of my commands are outdated and/or susceptible to injection, but I'd rather stick with this for now (many reasons).
I just had a question about trying to use SELECT from WHILE, and figured that out and got it to echo the correct response on the page.
Now, how do I make it echo that as a value for an HTML text box? It won't work, and I've tried to look for typos but I don't know what I am doing, frankly.
I see that the $studentid and $teacherinfo show fine, I presume because they are normal variables.
Can I somehow define two more variables for first name and last name further up in the page so that I do not need to include so much code in each input (and to keep it from being buggy)?
Here is my code for the page. The inputs will be hidden, but I have been making them text boxes for debugging purposes.
<?php
$connection = mysql_connect($serverName, $userName, $password) or die('Unable to connect to Database host' . mysql_error());
$dbselect = mysql_select_db($dbname, $connection) or die("Unable to select database:$dbname" . mysql_error());
$studentid = $_POST['student_id'];
$teacherinfo = $_POST['teacher'];
$result = mysql_query("SELECT `first_name` FROM `students` WHERE student_id = '$studentid'",$connection);
?>
</head>
<body>
<div align="center">
<form method="post" action="vote_post.php">
<h1>Vote for Teacher of the Month</h1>
<h4>(step 2 of 2)</h4>
<h2>Confirm the Information Below</h2>
<h5>Student id: <?php echo $studentid ?></br>
Student first name: <?php
while($row = mysql_fetch_array($result)){
echo $row['first_name'];
}
?>
</br>
Voted for: <?php echo $teacherinfo ?>
</h5>
<input type="text" name="student_id" value="<?php echo $studentid; ?>"/></br>
<input type="text" name="first_name" value="<?php while($row = mysql_fetch_array($result)){
echo $row['first_name'];
} ?>"/>
</br>
<input type="text" name="last_name" value="<?php while($row = mysql_fetch_array($result)){
echo $row['last_name'];
} ?>"/>
</br>
<input type="text" name="teacher" value="<?php echo $teacherinfo; ?>"/></br>
<input type="submit" value="Submit Vote" class="inputbutton"/></br></br></br>
</form>
You can't use while because your query return only one student. You have to use if instead of while. If your query return many students you can use while.
Try this code:
<?php
if($row = mysql_fetch_array($result)){
?>
Student first name: <?php echo $row['first_name'];?>
</br>
Voted for: <?php echo $teacherinfo ?></h5>
<input type="text" name="student_id" value="<?php echo $studentid; ?>"/></br>
<input type="text" name="first_name" value="<?php echo $row['first_name'];?>"/></br>
<input type="text" name="last_name" value="<?php echo $row['last_name'];?>"/></br>
<input type="text" name="teacher" value="<?php echo $teacherinfo; ?>"/></br>
<input type="submit" value="Submit Vote" class="inputbutton"/></br></br></br>
<?php
}
?>
I hope this help.
I tried to build a code that will help you. Remember that you use last_name, but does not return the field in SQL.
</head>
<body>
<div align="center">
<form method="post" action="vote_post.php">
<h1>Vote for Teacher of the Month</h1>
<h4>(step 2 of 2)</h4>
<h2>Confirm the Information Below</h2>
<?php
$connection = mysql_connect($serverName, $userName, $password) or die('Unable to connect to Database host' . mysql_error());
$dbselect = mysql_select_db($dbname, $connection) or die("Unable to select database:$dbname" . mysql_error());
$studentid = $_POST['student_id'];
$teacherinfo = $_POST['teacher'];
$result = mysql_query("SELECT `first_name`,`last_name`,`student_id` FROM `students` WHERE student_id = $studentid",$connection);
while($row = mysql_fetch_array($result)){
echo "<h5>Student id: $row['student_id'] </br>" .
"Student first name: $row['first_name'] </br>" .
"Voted for: $teacherinfo </h5> " .
"<input type='text' name='student_id' value='$row[\'student_id\']' /></br>" .
"<input type='text' name='first_name' value='$row[\'first_name\']' /></br>" .
"<input type='text' name='last_name' value='$row[\'last_name\']' /></br>" .
"<input type='text' name='teacher' value='$teacherinfo' /></br>"
}
?>
<input type="submit" value="Submit Vote" class="inputbutton"/></br></br></br>
</form>
WHILE I left because I do not know if your query can return more than one record, despite appearing to be a key. If you do not need to check the response of the Ragnar.

Sending id through select menu using PHP

here is my problem, I want to send id number through select menu using PHP.
Here is the code:
<form name="update" method="post" action="ex_update.php?id=<?php echo ((int)$_POST['get_id']); ?>">
<p><strong>Enter Name:</strong>
<input type="text" name="name">
<br />
ID:
<label for="select"></label>
<select name="get_id">
<?php
$query = "SELECT * FROM test";
$run = mysql_query($query);
while($output = mysql_fetch_array($run)){
echo "<option value=\"{$output['id']}\">{$output['id']}</option>";}
?>
</select>
</p>
<p>
<input type="submit" name="submit" value="Update!">
</p>
</form>
I have tried but when I submit the id in the URL equals to zero. how can I send id to the URL??
here is the ex_update.php >>>
<?php
$connect = mysql_connect("localhost","root","");
$sel_database = mysql_select_db("test");
$id = (int)$_GET["get_id"];
$name = mysql_real_escape_string( $_POST["name"] );
$query = "UPDATE test SET name='{$name}' WHERE id=={$id}";
if($run = mysql_query($query)){
}else{mysql_error();}
?>
Thanks in advance
You can use the form GET method
<form name="update" method="GET" action="ex_update.php">
You can access that select box value using $_GET['get_id'] in ex_update.php
Here is your First Page
Note action of form...
<form name="update" method="post" action="ex_update.php">
<p><strong>Enter Name:</strong>
<input type="text" name="name">
<br />
ID:
<label for="select"></label>
<select name="get_id">
<?php
$query = "SELECT * FROM test";
$run = mysql_query($query);
while($output = mysql_fetch_array($run)){
echo "<option value=\"{$output['id']}\">{$output['id']}</option>";}
?>
</select>
</p>
<p>
<input type="submit" name="submit" value="Update!">
</p>
</form>
And here you can find ex_update.php. Note: $id = (int)$_POST["get_id"];
<?php
$connect = mysql_connect("localhost","root","");
$sel_database = mysql_select_db("test");
$id = (int)$_POST["get_id"];
$name = mysql_real_escape_string( $_POST["name"] );
$query = "UPDATE test SET name='{$name}' WHERE id={$id}";
if($run = mysql_query($query)){
}else{mysql_error();}
?>

Categories