I'm trying to call the old values to be edited. What part am I wrong at?
<?php
if (isset($_GET['edit'])) {
$id = $_GET['edit'];
$update = true;
$record = mysqli_query($db, "SELECT * FROM bookinfo WHERE BookNo='$BookNo'");
if (mysqli_num_rows($record) == 1 ) {
$n = mysqli_fetch_array($record);
$BookNo = $n['BookNo'];
$ISBN = $n['ISBN'];
$title = $n['title'];
$author = $n['author'];
$publisher = $n['publisher'];
$status = $n['status'];
$cost = $n['cost'];
}
}
?>
<a href="viewBook.php?edit=<?php echo $row['BookNo']; ?>" class="edit_btn" >Edit</a>
</td>
<?php
if (isset($_GET['edit'])) { ?>
<form method="post" action = "viewBook.php">
<input type="hidden" name="BookNo" value="<?php echo $BookNo; ?>">
<input type="text" name="ISBN" value="<?php echo $ISBN; ?>">
<input type="text" name="title" value="<?php echo $title; ?>">
<input type="text" name="author" value="<?php echo $author; ?>">
<input type="text" name="publisher" value="<?php echo $publisher; ?>">
<input type="text" name="status" value="<?php echo $status; ?>">
<input type="text" name="cost" value="<?php echo $cost; ?>">
<?php if ($update == true): ?>
<button class="btn" type="submit" name="update" style="background: #556B2F;" >update</button>
<?php else: ?>
<button class="btn" type="submit" name="save" >Save</button>
<?php endif ?>
<?php } ?>
</form>
So far, what it does is, when the user clicks the edit button, it just shows 6 text fields. I thought by doing what I did, it was supposed to show the details already filled in the textbox.
When you do
$record = mysqli_query($db, "SELECT * FROM bookinfo WHERE BookNo='$BookNo'");
$BookNo is not defined.
maybe you wanted to do something like this:
$id = $_GET['edit'];
$update = true;
$record = mysqli_query($db, "SELECT * FROM bookinfo WHERE BookNo='$id'");
<form method="post" action = "viewBook.php">
your form method is "post" but you are checking $_GET You must check $_POST
if (isset($_GET['edit']))
you are passing value in $id And using $BookNo which not define.
only 6 input field will be show because first one is using hidden property.
<input type="hidden" name="BookNo" value="<?php echo $BookNo; ?>">
when you click on submit button data will be receive by $_POST
Related
I'm trying to update multiple rows using a form and a foreach loop. I've tried solutions found on stackoverflow, but just can't seem to get it to work. I'm working on page project.php?id=13
My HTML form:
<form method="post" action="project.php?id=<?php echo $id; ?>">
<?php while($row = $shops->fetch_assoc()) : ?>
<input type="hidden" name="shop[]" value="<?php echo $row['id']; ?>">
<input type="text" name="boxposition[]" class="form-control" value="<?php echo $row['box_position']; ?>" style="width: 50px;">
<input type="submit" name="update" value="Update Order" class="btn btn-primary">
<?php endwhile; ?>
</form>
My PHP code (on the same page) which is executed on if(isset($_POST['update'])) :
$shopid = $_POST['shop'];
$boxorder = $_POST['boxposition'];
foreach ($shopid as $index => $value) {
$query = "UPDATE shops SET box_position = '".$boxorder[$index]."' WHERE id = '".$shopid[$index]."'";
$update_row = $db->update($query);
Unfortunately, it only updates the first row (lowest id). The very strange thing is that I get the correct values when I echo them using. E.g.:
echo $shopid[$index].'<br>';
echo $boxorder[$index].'<br>';
An example:
I update the $boxorder for 2 rows:
Row 1 (for shop 1): new value = 6 (old value was 7)
Row 2 (for shop 2): new value = 8 (old value was 5)
Using foreach to echo the result, it shows the correct values:
1 (shop id)
6 (new boxorder value)
2 (shop id)
8 (new boxorder value)
What am I missing? Thanks in advance for helping me out!
Try following
<?php while($row = $shops->fetch_assoc()) : ?>
<form method="post" action="project.php?id=<?php echo $id; ?>">
<input type="hidden" name="shop[]" value="<?php echo $row['id']; ?>">
<input type="text" name="boxposition[]" class="form-control" value="<?php echo $row['box_position']; ?>" style="width: 50px;">
<input type="submit" name="update" value="Update Order" class="btn btn-primary">
</form>
<?php endwhile; ?>
to
<form method="post" action="project.php?id=<?php echo $id; ?>">
<?php while($row = $shops->fetch_assoc()) : ?>
<input type="hidden" name="shop[]" value="<?php echo $row['id']; ?>">
<input type="text" name="boxposition[]" class="form-control" value="<?php echo $row['box_position']; ?>" style="width: 50px;">
<input type="submit" name="update" value="Update Order" class="btn btn-primary">
<?php endwhile; ?>
</form>
$shopid = $_POST['shop'];
$boxorder = $_POST['boxposition'];
$query = "UPDATE shops SET box_position =? WHERE id =?";
$db->prepare($query);
foreach ($shopid as $index => $value) {
$db->execute(array($boxorder[$index], $shopid[$index]));
}
This should do it. This way you prepare the statement with ? and fill it inside your for each.
you can do it with also as the following :
$query = "UPDATE shops SET box_position = :position WHERE id = :id ";
$db->prepare($query);
foreach ($shopid as $index => $value) {
$db->execute(
array(
':position' => $boxorder[$index],
':id' => $shopid[$index]
)
);
}
i am coding a page for the attendance of students and in the page all the students name are retrieved dynamically from database using while loop and inside the while loop i have used a radio button.
my problem is the radio button work's correctly but i am unable to insert the value of radio buttons into database.here is my code :
include'connection.php';
#$id = mysql_real_escape_string($_GET['id']);
$res=mysql_query("SELECT * FROM `std_register` AS p1 ,`sims-register-course` AS p2 WHERE p2.semester=p1.std_semester and p2.department=p1.std_department and p2.id = '".$id."'");
if ($res==FALSE) {
echo die(mysql_error());
}
while ($row=mysql_fetch_assoc($res)) { ?>
<input type="hidden" id="dept_0" name="dept[]" value="<?php echo $row["department"] ?>">
<input type="hidden" id="course_name_1" name="course_name[]" value="<?php echo $row["course_name"] ?>">
<input type="hidden" id="std_semester_2" name="std_semester[]" value="<?php echo $row["semester"] ?>">
<input type="hidden" id="std_id_3" name="std_id[]" value="<?php echo $row["id"] ?>">
<input type="hidden" id="std_name_4" name="std_name[]" value="<?php echo $row["std_name"] ?>">
<tr>
<td class =info><?php echo $row["std_name"];?></td>
<td><input type="radio" name="<?php echo "status['".$row["std_name"]."']" ?>" value="1"></td>
<td><input type="radio" name="<?php echo "status['".$row["std_name"]."']" ?>" value="2"></td>
</tr>
<?php }?>
</table><br>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-3">
<input type="submit" name="submit" value="Assign" class="btn btn-large btn-block btn-primary"></div></div>
</form>
<?php
for ($i=0; $i < 4; $i++) {
if(isset($_POST["submit"])) {
$stdid = mysql_real_escape_string($_POST['std_id'][$i]);
$dptname = mysql_real_escape_string($_POST['dept'][$i]);
$courseName = mysql_real_escape_string($_POST['course_name'][$i]);
$stdSemester = mysql_real_escape_string($_POST['std_semester'][$i]);
$std_name = mysql_real_escape_string($_POST['std_name'][$i]);
$present = mysql_real_escape_string($_POST['status'][$i]);
$totalClasses = $present;
$insrt = mysql_query("INSERT INTO `attendence_student` (department_name,courseName,semester,present,absent,total_classes) VALUES('$dptname','$courseName','$stdSemester','$present',0,0)") or die(mysql_error());
}
}
and is it possible to insert values from this radio buttons to two columns of database?
I am not sure but try to add 'checked' property in radio input
<input type="radio" name="<?php echo "status[".$row["std_name"]."]" ?>" value="1" checked></td>
Or
use Select tag instead of radio button
EDIT
use this code to insert in present column or absent column
$value = mysql_real_escape_string($_POST['status'][$i]);
$totalClasses = $value;
if($value == 1) //check if present
{
$sql="INSERT INTO `attendence_student` (department_name,courseName,semester,present,absent,total_classes) VALUES('$dptname','$courseName','$stdSemester','$value',0,0)";
}
else //if absent
{
$sql="INSERT INTO `attendence_student` (department_name,courseName,semester,present,absent,total_classes) VALUES('$dptname','$courseName','$stdSemester',0,'$value',0)";
}
$insrt = mysql_query($sql) or die(mysql_error());
I'm making a site where the owner has to be able to update their events, but my update code isnt working even though im 99% sure I havent made any errors.
First the form where you press update:
<?php
$sql = "SELECT * FROM events ORDER BY id ASC";
$res = $objCon->query($sql) or die('fejl i query:'.mysqli_error($objCon));
while($row=$res->fetch_array()) {
$id = $row['id'];
echo "<div class='eventpost'>";
echo "<div class='dato'>";
echo $row['id'];
echo "</div>";
echo "<p class='overskrift'>";
echo "<a href='update.php?id=$id'>RET </a>";
echo "<a href='code_delete.php?id=$id'>SLET</a>";
echo $row['overskrift'];
echo "</p>";
echo "</div>";
}
?>
then the update form:
<form action="code_update.php" method="POST">
<label>Dato:<br>
<input type="text" name="dag" value="<?php echo $data['dag']; ?>"></label>
<label>MÃ¥nede:<br>
<input type="text" name="month" value="<?php echo $data['month']; ?>"></label>
<label>Overskrift:<br>
<input type="text" name="overskrift" value="<?php echo $data['overskrift']; ?>"></label>
<label>Tekst:<br>
<input type="text" name="tekst" value="<?php echo $data['tekst']; ?>"></label>
<input type="hidden" name="id" value="<? echo $id; ?>">
<input type="submit" value="Opret">
</form>
and finally the update code
<?php
session_start();
if($_SESSION['auth'] == 2){
include('incl_db.php');
$id = $_POST['id'];
$overskrift = $_POST['overskrift'];
$dag = $_POST['dag'];
$month = $_POST['month'];
$tekst = $_POST['tekst'];
$sql = "UPDATE events SET overskrift='$overskrift', dag='$dag', month='$month', tekst='$tekst' WHERE id='$id'";
$res = $objCon->query($sql);
header('location:events.php');
}else{
header('location:index.php');
}
?>
Probably shorthand tags are disabled in your php version.So try changing this
<input type="hidden" name="id" value="<? echo $id; ?>">
to this
<input type="hidden" name="id" value="<?php echo $id; ?>">
You can check this answer for more.
as far as i can see my code is sound however, I keep getting an error
this is the error
Notice: Undefined variable: person in
\sql\modify.php on line 12
here is my code..
<?php
include 'includes/connection.php';
if (!isset($_POST['submit'])){
$q = "SELECT * FROM people WHERE ID = $_GET[id]";
$result = mysql_query($q);
$person = mysql_fetch_array($result);
}
?>
<h1>You are modifying A User</h1>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
Name<input type="text" name="inputName" value="<?php echo $person['Name']; ?>" /><br />
Description<input type="text" name="inputDesc" value="<?php echo $person['Description']; ?>" />
<br />
<input type="hidden" name="id" value="<?php echo $_GET['id']; ?>" />
<input type="submit" name="submit" value="Modify"/>
</form>
<?php
if(isset($_POST['sumbmit'])) {
$u = "UPDATE people SET `Name` = '$_POST[inputName]', `Description` = '$_POST[inputDesc]' WHERE ID = $_POST[id]";
mysql_query($u) or die(mysql_error());
echo "User has been modify";
header("Location: index.php");
}
?>
any Thoughts or am im I just blind???
<?php
include 'includes/connection.php';
// set $person veriable
if (!isset($_POST['submit'])){
$q = "SELECT * FROM people WHERE ID = $_GET[id]";
$result = mysql_query($q);
$person = mysql_fetch_array($result);
}
// if form submit you use update and redirect
else {
$u = "UPDATE people SET `Name` = '$_POST[inputName]', `Description` = '$_POST[inputDesc]' WHERE ID = $_POST[id]";
mysql_query($u) or die(mysql_error());
//echo "User has been modify"; // this not need, bcz execute header('location') redirect you current page
header("Location: index.php");
exit(); //use it after header location
}
?>
<h1>You are modifying A User</h1>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
Name<input type="text" name="inputName" value="<?php echo $person['Name']; ?>" /><br />
Description<input type="text" name="inputDesc" value="<?php echo $person['Description']; ?>" />
<br />
<input type="hidden" name="id" value="<?php echo $_GET['id']; ?>" />
<input type="submit" name="submit" value="Modify"/>
</form>
You just need to check if you actually got output.
Just a plain example:
if ($person):
?>
<h1>You are modifying A User</h1>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
Name<input type="text" name="inputName" value="<?php echo $person['Name']; ?>" /><br />
Description<input type="text" name="inputDesc" value="<?php echo $person['Description']; ?>" />
<br />
<input type="hidden" name="id" value="<?php echo $_GET['id']; ?>" />
<input type="submit" name="submit" value="Modify"/>
</form>
<?php
endif;
Remember to always validate your input and output, but also if queries you try to run do produce a result set.
Here's the issue:
<?php
include 'includes/connection.php';
if (!isset($_POST['submit'])){
$q = "SELECT * FROM people WHERE ID = $_GET[id]";
$result = mysql_query($q);
$person = mysql_fetch_array($result);
}
?>
At this point $person is set only if the form hasn't been submitted; but later on:
Name<input type="text" name="inputName" value="<?php echo $person['Name']; ?>" /><br />
Description<input type="text" name="inputDesc" value="<?php echo $person['Description']; ?>" />
You're using it anyway. If the form has been submitted, then you're going to get the warning you're seeing. What you need to do is something like:
if (isset($_POST['submit'])){
$name = $_POST['name'];
$description = $_POST['description']
} else {
$q = "SELECT * FROM people WHERE ID = $_GET[id]";
$result = mysql_query($q);
$person = mysql_fetch_array($result);
$name = $person['name'];
$description= $person['description'];
}
And then:
Name<input type="text" name="inputName" value="<?php echo $name ?>" /><br />
Description<input type="text" name="inputDesc" value="<?php echo $description; ?>" />
The variables are now set either way.
A couple of other things - you're not doing any error checking to see if your query has worked; if the query fails, your code will carry on regardless.
Secondly, the mysql_ functions are deprecated and will stop working at some point; you should look at moving to using mysqli_* or PDO instead.
My GET value is fine on first if statement but when it hits update_submit I get an undefined variable error. This is still test script so there's no validation
<?php
session_start();
include "connect.php";
error_reporting(E_ALL);
ini_set('display_errors','1');
if(isset($_GET['pid']))
{
$get_item = $_GET['pid'];
echo "pid" . $get_item;//<--has value here
$get_products = $db->prepare("select * from `item` where
`item_id` = '$get_item' LIMIT 1");
$get_products->execute();
while ($row = $get_products->fetch())
{
$user_id = $row['user_id'];
$item_name = $row['item_name'];
$item_description = $row['item_description'];
$image = $row['photopath'];
}
}
echo "pid" .$get_item;//<---has value here
if(isset($_POST['cancel_edit']))
{
header("Location: manage_items.php");
exit();
}
if(isset($_POST['update_submit']))//<<---lose it here
{
$get_products = $db->prepare("select `photopath` from `item` where
`item_id` = '$get_item' LIMIT 1");
$get_products->execute();
$path= $get_products->fetchColumn(6);
FORM
<form action="item_edit.php" method="post" enctype="multipart/form-data">
<input type = "text" name="item_name" value="<?php echo $item_name ?> "/>
<textarea name="item_description"><?php echo $item_description ?></textarea>
<p> <img src="<?php echo $image; ?>" width="75" height="75" /></p>
<input type="file" name="image_edit" value="<?php echo $image ?>"/>
<input name="img_edit" type="hidden" value="<?php echo $image ?>"/>
<input name="edit_form_id" type="hidden" value="<?php echo $get_item ?>">
<p><input type="submit" name="update_submit" value="Update"/></p>
<p><input type="submit" name="cancel_edit" value="Cancel"/></p>
</form>
If coming from the same form, are you mixing $_GET and $_POST settings for your form? You have the value when using ($_GET['pid']), but not when using ($_POST['update_submit']).