trying to update mysql database in php - php

I'm trying to have an html form which updates mysql data. Now , I have this code(which is also a form action) and I'm trying to also use this as a form for my update. Because I will need the data that this form would show, so that it will be easier for the users to update only what they wish to update.
this is the form that will try to search the data :
<form name="form1" method="post" action="new.php">
<td>
<table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF">
<tr>
<td colspan="16" style="background:#9ACD32; color:white; border:white 1px solid;
text-align: center"><strong><font size="3">ADMISSION INFORMATION SHEET</strong></td>
</tr>
<tr>
This is new.php( will display the corresponding data based on the firstname inputted. And will also try to serve as a form for the update process.
$con = mysql_connect("localhost","root","");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("Hospital", $con);
$result = mysql_query("SELECT * FROM t2 WHERE FIRSTNAME='{$_POST["fname"]}'");
?>
<table width="900" border="0" align="left" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
<td>
<table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF">
<tr>
<td colspan="16" style="background:#9ACD32; color:white; border:white 1px solid; text-align: center"><strong><font size="3">ADMISSION INFORMATION SHEET</strong></td>
</tr>
<tr>
<?php while ( $row = mysql_fetch_array($result) ) { ?>
<form name="form1" method="post" action="update.php">
<td width="54"><font size="3">Hospital #</td>
<td width="3">:</td>
<td width="168"><input name="hnum" type="text" value="<?php echo $row["HOSPNUM"]; ?>">
</td>
This is my update.php,
mysql_select_db("Hospital", $con);
mysql_query("UPDATE t2 SET HOSPNUM='$_POST[hnum]' ROOMNUM='$_POST[rnum]',
LASTNAME='$_POST[lname]', FIRSTNAME='$_POST[fname]', MIDNAME='$_POST[mname]',
CSTAT='$_POST[cs]' AGE='$_POST[age]', BDAY='$_POST[bday]', ADDRESS='$_POST[ad]',
STAT='$_POST[stats1]', STAT2'$_POST[stats2]', STAT3'$_POST[stats3]',
STAT4'$_POST[stats4]', STAT5'$_POST[stats5]', STAT6'$_POST[stats6]',
STAT7'$_POST[stats7]', STAT8'$_POST[stats8]', NURSE='$_POST[nurse]', TELNUM
='$_POST[telnum]'
WHERE FNAME ='$_POST[fname]'");
mysql_close($con);
?>
-Please help, I don't have any idea why it isnt updating the data.

Typo, there is a missing "," between HOSPNUM and ROOMNUM:
SET HOSPNUM='$_POST[hnum]', ROOMNUM=

The previous comments are absolutely correct. I would recommend using the PDO or MySQLi adapters and use a prepared statement for your record insertion as a bare minimum of security. Using the first name as a unique identifier is a bad idea. Don't you have a primary key column in the table?
To answer your actual question, one the problem is with the array notation in the double-quoted string. There are several equals signs missing from your statement as well. Try this:
mysql_query("
UPDATE t2
SET HOSPNUM='" . mysql_real_escape_string($_POST['hnum']) . "',
ROOMNUM='" . mysql_real_escape_string($_POST['rnum']) . "',
LASTNAME='" . mysql_real_escape_string($_POST['lname']) . "',
FIRSTNAME='" . mysql_real_escape_string($_POST['fname']) . "',
MIDNAME='" . mysql_real_escape_string($_POST['mname']) . "',
CSTAT='" . mysql_real_escape_string($_POST['cs']) . "',
AGE='" . mysql_real_escape_string($_POST['age']) . "',
BDAY='" . mysql_real_escape_string($_POST['bday']) . "',
ADDRESS='" . mysql_real_escape_string($_POST['ad']) . "',
STAT='" . mysql_real_escape_string($_POST['stats1']) . "',
STAT2='" . mysql_real_escape_string($_POST['stats2']) . "',
STAT3='" . mysql_real_escape_string($_POST['stats3']) . "',
STAT4='" . mysql_real_escape_string($_POST['stats4']) . "',
STAT5='" . mysql_real_escape_string($_POST['stats5']) . "',
STAT6='" . mysql_real_escape_string($_POST['stats6']) . "',
STAT7='" . mysql_real_escape_string($_POST['stats7']) . "',
STAT8='" . mysql_real_escape_string($_POST['stats8']) . "',
NURSE='" . mysql_real_escape_string($_POST['nurse']) . "',
TELNUM='" . mysql_real_escape_string($_POST['telnum']) . "'
WHERE FNAME='" . mysql_real_escape_string($_POST['fname']) . "'
");

Related

How to change column name into a variable

I've written a function which is gonna be used by my team and so far there are no specific actual column names in it except the columns in a **foreach loop**. I'd like to change them in to a variable so my team just change the variables in the given array instead of searching the entire code. Here's a snippet so you can see what I mean.
These are the attributes where you put in the actual name of the column.
$attributes = array("id", "firma", "vorname", "nachname", "straße", "hausnummer", "telefonnr", "dateien", "column", "column");
That's a row in my table which is displayed with echo "code" statement in php. Everytime I change the column name which is used by the foreach loop I'll get an error.
<tr> <!-- display data -->
<td style='text-align:center;' width=" . $width[0] . ">$print->id</td>
<td width=" . $width[1] . ">$print->firma</td>
<td width=" . $width[2] . ">$print->vorname</td>
<td width=" . $width[3] . ">$print->nachname</td>
<td width=" . $width[4] . ">$print->straße</td>
<td style='text-align:center;' width=" . $width[5] . ">$print->hausnummer</td>
<td width=" . $width[6] . ">$print->telefonnr</td>
What I tried so far was
<td width=" . $width[1] . ">$print->" . $attributes[1] . "</td>
or
<td width=" . $width[1] . ">$print->$attributes[1]</td>
Is there a way to change the column name into a variable without getting errors?

Printing Horizontal Tables Including Images Using PHP MySQL

I am trying to print out on my sites home page a horizontal table which includes a picture of a car and then below the make, model & price of the car.
Here's roughly what I have done:
<?php
$List = "";
$sql = mysql_query("SELECT * FROM Car ORDER BY listed DESC LIMIT 5");
$Count = mysql_num_rows($sql);
if ($Count > 0) {
while($row = mysql_fetch_array($sql)){
$id = $row["id"];
$make = $row["make"];
$model = $row["model"];
$price = $row["price"];
$List .= '<table width="100%" border="0" cellspacing="0" cellpadding="6">
<tr>
<td width="20%" valign="top"><a href="/motors/cars.php?id=' . $id . '"><img style="border:#666 1px solid;"
src="/../../motors-' . $id . '.jpg" alt="' . $status . ' ' . $title . '" width="100" height="80" border="1" /></a></td>
</tr>
<tr>
<td width="80%" valign="top" align="left"><font face="Arial" color="#000080"><B>
' . $make . ' ' . $model . '</B></font><br />' . $price . '
view car details</align></td>
</tr>
</table>';
}
} else {
$List = "No cars at present";
}
mysql_close();
?>
Can anyone help me sort this code to print out horizontally? Many Thanks!
The src of the image is most likely wrong, /../../motors... is still /motors...
you can move the images into your webapps /motors/images folder and set src as /motors/images/motors...
The table you create must be nested in another table.
<table>
<tr>
<td>
<table of a car>
</td>
<td>
<table of a car>
</td>
<td>
<table of a car>
</td>
.... etc.
</tr>
</table>
It will make sense to emit a tr every few cars to wrap to a new line.
table of a car is the html you collect in $List.

How to display multiple rows one after the other repeating a sequence depending on how many rows I get?

My idea is to create a Q&A section under a product profile, just like on eBay or Amazon or whatever. The idea is to send a question and then get the owner of the article to reply.
The table has these columns: pid (product ID), id (question ID), question, answer, date (date posted), username.
So if I post a question, I get the ID of the product in which I'm posting and create a question. The the owner just sends the answer to the row that matches the question.
Here's my PHP code to retrieve all the info from that table:
$qanda = '';
$link = mysql_connect("localhost", "youknowwhat", "youknowwhat");
mysql_select_db("youknowwhat", $link);
$qandaq = mysql_query("SELECT * FROM questions WHERE id='$id2' ORDER BY date", $link);
$count = mysql_num_rows($qandaq);
if($count >= 1){
while($rows = mysql_fetch_array($qandaq)){
$date = $rows['date'];
$q = $rows['question'];
$a = $rows['answer'];
$usrname = $rows['username'];
}
$qanda .= '<div id="answers" align="center">
<table cellspacing="0" align="center">
<tr align="center">
<td width="200">' . $date . '</td>
<td rowspan="2" width="400"><strong>' . $q . '</strong><br>' . $a . '</td>
<td width="200">Delete</td>
</tr>
<tr align="center">
<td>' . $usrname . '</td>
<td>Report</td>
</tr>
</table>
</div>';
} else {
$qanda = '<div id="answers" align="center">
No questions for this product.
</div>';
}
Now... what you see as a table in the variable $qanda I want to repeat it over and over again but displaying different row data but the concatenation isn't working and I can only get the last row to be displayed. I just can't seem to find out why this isn't working! Am I missing something?
All you have to do is append your divs (.=) while you're inside the while loop that mysql_fetch_array() rows.
Then you'll have a new div for each row your database returns, and you can populate it easily.
$qanda = '';
while($rows = mysql_fetch_array($qandaq)){
$date = $rows['date'];
$q = $rows['question'];
$a = $rows['answer'];
$usrname = $rows['username'];
$qanda .= '<div id="answers" align="center">
<table cellspacing="0" align="center">
<tr align="center">
<td width="200">' . $date . '</td>
<td rowspan="2" width="400"><strong>' . $q . '</strong><br>' . $a . '</td>
<td width="200">Delete</td>
</tr>
<tr align="center">
<td>' . $usrname . '</td>
<td>Report</td>
</tr>
</table>
</div>';
}

Delete from html table made by database PHP

Here is the table below that I'm trying to delete rows out of:
<form method="POST" >
<table class="sortable">
<thead>
<tr>
<th id="makehead">Make </th>
<th id="modelhead">Model </th>
<th id="idhead">Delete </th>
</tr>
</thead>
<tbody>
<?php
$i = 0;
foreach ($carArray as $k => $carInfo) {
$i++;
echo '<tr>';
if ($i % 2) {
echo '<td class="make">' . $carInfo['make'] . '</td>
<td class="model">' . $carInfo['model'] . '</td>
<td class="id"><input type="checkbox" name="id" value="' . $carInfo['id'] . '">' . $carInfo['id'] . '</td>';
} else {
echo '<td class="makelight">' . $carInfo['make'] . '</td>
<td class="modellight">' . $carInfo['model'] . '</td>
<td class="idlight"><input type="checkbox" name="id" value="' . $carInfo['id'] . '">' . $carInfo['id'] . '</td>';
}
}
?>
</tr>
</table>
</tbody>
<td>
<input Onclick="return ConfirmDelete();" name="delete" type="submit" id="delete" value="Delete"></input>
</td>
</table></form>
As you can see i'm using checkboxes to tick each row then the delete button will have a confirm message then should delete but it doesn't here is my if statement:
if ($_REQUEST['delete']) {
$dbid = $_REQUEST['id'];
$db->setdbid($dbid);
So when this wasn't working I had a look on here and on other questions people said I need a setter function so i did this: EDIT: this is my class file.
public function setdbid($dbid){
$this->dbid=$dbid;
}
for this main function to delete things:
public function delete($dbid) {
try {
$sql = "DELETE FROM cars WHERE id = '$dbid'";
$this->db->exec($sql);
echo "Car has been deleted.";
} catch (PDOException $e) {
echo $e->getMessage();
}
}
So that's all the relevant code I think, please help me if you can.
You just have to replace some piece of code in PHP :
if ($_REQUEST['delete']) {
$dbid = $_REQUEST['id'];
$db->delete($dbid); //Assuming delete is well a $db method, else replace it by the correct delete call
}
As you are using checkboxes with the same name, you have to change it so it's an array (and this way you'll be able to delete multiple rows at once) :
<td class="id"><input type="checkbox" name="ids[]" value="' . $carInfo['id'] . '">' . $carInfo['id'] . '</td>';
Then in your php code, treat this data as such :
if ($_REQUEST['delete']) {
foreach($_REQUEST['ids'] as $id){
$xxx->delete(intval($id)); //convert to integer to avoid sql injection.
}
}
Note that you don't need to set $db->setdbid since you pass that id as a parameter of your delete method.

How do I pass values with a link to a new page?

What I want is to be able to click on the link (review.php) and then to display the review corresponding to that film only, on a new page.
Not sure if my MySQL query is wrong or if my A href link is not formatted correctly.
Any help would be appreciated.
require_once('./includes/mysql_connect.php');
$query = "SELECT films.movie_title, films.rating, films.actor, reviewed.review
FROM films
INNER JOIN reviewed
ON films.movie_id=reviewed.review_id";
$result = mysql_query($query) or die ("Could not execute mysql" . mysql_error()); // Run Query
$num = mysql_numrows($result);
if ($num > 0) { // If it ran ok, display records.
echo "<p> There are curently $num records.</p>";
// Table header.
echo '<table border="1" align="center" cellspacing="0" cellpadding="5">
<tr>
<td align="left"><b>Movie Title</b></td>
<td align="left"><b>Leading Actor</b></td>
<td align="left"><b>Rating</b></td>
<td align="left"><b>Review</b></td>
</tr>';
// Fetch and print all the records.
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
echo '<tr>
<td align="left">' . $row['movie_title'] . '</td>
<td align="left">' . $row['actor'] . '</td>
<td align="left">' . $row['rating'] . '</td>
<td align="left"><a href="review.php?id='. $row['review'] . '> Read Review </a>
</tr> ';
}
}
?>

Categories