PHP page with MySQL not showing/working - php

The following code is not working as the page displays nothing, and I am not exactly sure why. It gets a few things from the URL and then the final Album name from the database. Here is the code:
<?php
$cart1 = rawurldecode($_GET["path"]);
list( , , , , , $cart2) = explode ("\\", $cart1);
$cart3 = $cart2;
list($cart4) = explode (" ", $cart3);
$con = mysql_connect("SERVER","USER","PASS");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("cartmatch", $con);
$result = mysql_query("SELECT * FROM cartmatch WHERE CARTNO='$cart4'");
while($row = mysql_fetch_array($result))
{
echo '<form enctype="multipart/form-data" action="albumgo.php" method="POST"><input name="ID" type="hidden" value=';
echo $_GET["ID"];
echo ' ><input name="enabled" type="hidden" value=';
echo $_GET["enabled"];
echo ' ><input name="artist" type="hidden" value=';
echo $_GET["artist"];
echo ' ><input name="title" type="hidden" value="';
echo $_GET["title"];
echo '" >Name:<br/><input name="album" type="text" autofocus="autofocus" value="';
echo $row['ALBUM'];
echo '" ><input type="submit" name="edit" value="Save"></form>';
}
mysql_close($con);
?>

Try to put the following code to see if there is an error in your script.
ini_set('display_errors', 1);
ini_set('log_errors', 1);
ini_set('error_log', dirname(__FILE__) . '/error_log.txt');
error_reporting(E_ALL);
Probably there is a database connection error or something preventing PHP from displaying the rest of the content.

list($cart4) = explode (" ", $cart3);
Should have been
list($cart4) = explode ("+", $cart3);

Related

How to update MySql data from HTML using PHP?

I am trying to update mysql database from html table with php, but when edit the row that i want to update and press the button to update, field that was suposed to updated becomes empty.
Also when i press update i get this notice:
Notice: Undefined index: status in /storage/ssd1/314/2412314/public_html/status3.php on line 174.
This is my code:
<?php
$db_host='example';
$db_user='example';
$db_pass='example';
$db_name='example';
$con = mysqli_connect($db_host, $db_user, $db_pass, $db_name);
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
if(isset($_POST['update']))
{
$STATUS = $_POST['status'];
$PK= $_POST['pkvara'];
$sql = "UPDATE Radionica SET status = '$STATUS' WHERE pkvara = '$PK'";
$retval = mysqli_query($con,$sql );
if(! $retval )
{
die('Could not update data: ' . mysql_error());
}
echo "Status uspešno promenjen\n";
}
$result = mysqli_query($con,"SELECT * FROM Radionica")
or die("Error: ".mysqli_error($con));
while($row = mysqli_fetch_array($result))
{
echo '<form action="" method="post">';
echo '<tr>';
echo '<td>'.$row['registracija'].'</td>';
echo '<td>'.$row['status'].'</td>';
echo '<td><input type="text" name="sta" value="'.$row['status'].'"><input type="submit" name="update" value="Promeni" /></td>';
echo'<td><input type="hidden" name="pkvara" value="'.$row['pkvara'].'"></td>';
echo '</tr>';
echo '</form>';
}
mysqli_close($con);
?>
Fix this line and it will work fine.
echo '<td><input type="text" name="status" value="'.$row['status'].'"><input type="submit" name="update" value="Promeni" /></td>';
correct this line
echo '<td><input type="text" name="sta" value="'.$row['status'].'"><input type="submit" name="update" value="Promeni" /></td>';
TO
echo '<td><input type="text" name="STATUS" value="'.$row['status'].'"><input type="submit" name="update" value="Promeni" /></td>';

php update mysql table via form, reload information on page immediately

I'm creating a page in which room reservations are displayed in a table, with the possibilty to update or delete them.
The reservations come from a MySQL-database, table reservations.
It works, but I would like that the information from the database is updated on the page immediately after pressing the buttons.
For instance, if now I set the username from 'person' to 'another person', the field gets updated correctly in the database, but I need to refresch the page to see the update in my table.
How can I do this?
<table border="1">
<tr><td>Datum</td><td>Start</td><td>Stop</td><td>Gebruikersnaam</td></tr>
<?php
$now = date("Y-m-d");
$query = "SELECT * FROM reservations WHERE (roomid = " . 45 . " AND end > NOW() ) ORDER BY start";
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result)){
$reservationid=$row["reservationid"];
$username=$row["username"];
$aantal=$row["numberingroup"];
$reservationid=$row["reservationid"];
$start=$row["start"];
$end=$row["end"];
$roomid=$row["roomid"];
?>
<form action="" method="post">
<tr><td><input name="StartDate" value="<? echo $StartDate; ?>" /></td><td><input name="StartTime" value="<? echo $StartTime; ?>" /></td><td><input name="StopTime" value="<? echo $StopTime; ?>" /></td><td><input name="username" value="<? echo $username;?>" /></td><td><input type="submit" value="update" name="<?php echo 'update_' . $reservationid; ?>" /></td><td><input type="submit" value="delete" name="<?php echo 'delete_' . $reservationid; ?>" /></td><td><? echo $reservationid; ?></td></tr></form>
<?php
//DELETE
if(isset($_POST['delete_' . $reservationid]))
{
$deletequery = "DELETE FROM reservations WHERE reservationid=" . $reservationid;
if(mysql_query($deletequery)){
echo "<p><b>Boeking verwijderd</b></p>";}
else {
echo "<p><b>Boeking niet verwijderd</b></p>";}
}
//UPDATE
if(isset($_POST['update_' . $reservationid]))
{
$NewStartDate = explode("-",$_POST[StartDate]);
$newstartdate = $NewStartDate[2] . "-" . $NewStartDate[1] . "-" . $NewStartDate[0];
$newstarttime = $_POST[StartTime] . ":00";
$newenddate = $newstartdate;
$NewEndTime = explode(":",$_POST[StopTime]);
$newendtime = mktime($NewEndTime[0],($NewEndTime[1]-1),59);
$newendtime = date("H:i:s",$newendtime);
$UpdateStart = $newstartdate . " " . $newstarttime;
$UpdateEnd = $newenddate . " " . $newendtime;
$UpdateUsername = $_POST[username];
$updatequery = "UPDATE reservations SET start='$UpdateStart', end='$UpdateEnd', username='$UpdateUsername' WHERE reservationid=" . $reservationid;
if(mysql_query($updatequery)){
echo "<p><b>Updated " . $reservationid . " " . $UpdateStart . " " . $UpdateEnd . " " . $UpdateUsername . "</b></p>";}
else {
echo "<p><b>FAILURE IS NOT AN OPTION. AGAIN!</b></p>";}
}
?>
<?php
}
mysql_close();
?>
The working code is:
<?php
//DELETE
if(isset($_POST['delete_' . $_POST[updateid]]))
{
$deletequery = "DELETE FROM reservations WHERE reservationid=" . $_POST[updateid];
if(mysql_query($deletequery)){
echo "<p><b>Boeking verwijderd</b></p>";
}
else {
echo "<p><b>FAILURE IS NOT AN OPTION. AGAIN!</b></p>";
}
}
//UPDATE
if(isset($_POST['update_' . $_POST[updateid]]))
{
$UpdateID = $_POST[updateid];
$NewStartDate = explode("-",$_POST[StartDate]);
$newstartdate = $NewStartDate[2] . "-" . $NewStartDate[1] . "-" . $NewStartDate[0];
$newstarttime = $_POST[StartTime] . ":00";
$newenddate = $newstartdate;
$NewEndTime = explode(":",$_POST[StopTime]);
$newendtime = mktime($NewEndTime[0],($NewEndTime[1]-1),59);
$newendtime = date("H:i:s",$newendtime);
$UpdateStart = $newstartdate . " " . $newstarttime;
$UpdateEnd = $newenddate . " " . $newendtime;
$UpdateUsername = $_POST[username];
$updatequery = "UPDATE reservations SET start='$UpdateStart', end='$UpdateEnd', username='$UpdateUsername' WHERE reservationid='$UpdateID'";
if(mysql_query($updatequery)){
echo "<p><b>Updated " . $reservationid . " " . $UpdateStart . " " . $UpdateEnd . " " . $UpdateUsername . "</b></p>";
}
else {
echo "<p><b>FAILURE IS NOT AN OPTION. AGAIN!</b></p>";
}
// echo "<p><b>Updated " . $reservationid . " " . $UpdateStart . " " . $UpdateEnd . " " . $UpdateUsername . "</b></p>";
}
?>
<?php
$query = "SELECT * FROM reservations WHERE (roomid = " . 45 . " AND end > NOW() ) ORDER BY start";
$result = mysql_query($query) or die(mysql_error());
?>
<table border="1">
<tr><td>Datum</td><td>Start</td><td>Stop</td><td>Gebruikersnaam</td></tr>
<?php
while($row = mysql_fetch_array($result)){
$reservationid=$row["reservationid"];
$username=$row["username"];
$aantal=$row["numberingroup"];
$reservationid=$row["reservationid"];
$start=$row["start"];
$end=$row["end"];
$roomid=$row["roomid"];
$startdate = explode(" ",$start);
$startdate[0] = explode("-",$startdate[0]);
$startdate[1] = explode(":",$startdate[1]);
$StartFormat = mktime($startdate[1][0],$startdate[1][1],$startdate[1][2],$startdate[0][1],$startdate[0][2],$startdate[0][0]);
$StartDate = date("d-m-Y",$StartFormat);
$StartTime = date("H:i",$StartFormat);
$stopdate = explode(" ",$end);
$stopdate[0] = explode("-",$stopdate[0]);
$stopdate[1] = explode(":",$stopdate[1]);
$StopFormat = mktime($stopdate[1][0],$stopdate[1][1],($stopdate[1][2]+1),$stopdate[0][1],$stopdate[0][2],$stopdate[0][0]);
$StopDate = date("d-m-Y",$StopFormat);
$StopTime = date("H:i",$StopFormat);
?>
<form action="" method="post">
<tr><td><input type="hidden" name="updateid" value="<?php echo $reservationid; ?>" /> <input name="StartDate" value="<? echo $StartDate; ?>" /></td><td><input name="StartTime" value="<? echo $StartTime; ?>" /></td><td><input name="StopTime" value="<? echo $StopTime; ?>" /></td><td><input name="username" value="<? echo $username;?>" /></td><td><input type="submit" value="update" name="<?php echo 'update_' . $reservationid; ?>" /></td><td> <input type="submit" value="delete" name="<?php echo 'delete_' . $reservationid; ?>" /></td> </tr>
</form>
<?php
}
mysql_close();
?>
</table>
Move the logic that does the updating and deleting above the logic that does the rendering:
<?php
// DELETE (your delete stuff)
// UPDATE (your update stuff)
// RETRIEVE (your SELECT query)
?>
<table> <!-- your table markup -->
<?php
// RENDER (your while loop and such)
You'll also need to adjust your logic a bit. You're using the $reservationid from the SELECT to do the deleting and updating. This doesn't work, because the execution context for the PHP is refreshed with each page load. What you need is to store the reservation id in each form (maybe in a hidden field), and then to retrieve that from $_POST.
Incidentally, your code is very vulnerable to SQL injection. Also, you should look at using mysqli or PDO; mysql_connect is deprecated in the current version of PHP.
You could use jQuery for this. You have to make an $.ajax (http://api.jquery.com/jquery.ajax/) call. From the callback you can fill/set the fields you want to. You'll need $('#idofelement').html()(http://api.jquery.com/html/) for this. If you have got any questions don't be affraid to ask ;) Good luck!

PHP unable to write to MySQL database

I have gone over my code many many MANY times and added any missing brackets or semi-colons but still whenever I upload this code to my website and load the page I still get a completely blank screen. The code was from an O'Reilly book so I went and checked the website if there are any reported errors in the book but found nothing related to this particular example.
I don't feel like it's an issue with permissions because I think the page would at least report one of the errors I coded into it. Could it have to do with the versions of PHP or MySQL I am using? I was able to connect to the database in the past and query it but writing just isn't happening. I am at a complete loss at this point. All I want to do is write to my MySQL database and party :(
Here is the code:
<?php
require_once 'login.php';
// Create connection
$db_server = mysql_connect($db_hostname, $db_username, $db_password);
// Check connection
if (!$db_server) die("Unable to connect to MySQL: " . mysql_error());
mysql_select_db($db_database, $db_server)
or die("Unable to select database: " . mysql_error());
if (isset($_POST['delete']) && isset($_POST['avail']))
{
$avail = get_post('avail');
$query = DELETE FROM test WHERE avail='$avail';
if (!mysql_query($query, $db_server))
echo "DELETE failed: $query<br />" .
mysql_error() . "<br /><br />";
}
if (isset($_POST['id']) &&
isset($_POST['item_name']) &&
isset($_POST['avail']))
{
$id = get_post('id');
$item_name = get_post('item_name');
$avail = get_post('avail');
$query = "INSERT INTO test VALUES" .
"('$id','$item_name','$avail')";
if (!mysql_query($query, $db_server))
{echo "INSERT failed: $query<br />" .
mysql_error() . "<br /><br />";
}
}
?>
<form action="index.php" method="post"><pre>
Line Number <input type="text" name="id" />
Product Name <input type="text" name="item_name" />
Quantity Available <input type="text" name="avail" />
<input type="submit" value="ADD RECORD" />
</pre></form>
<?php
$query = "SELECT * FROM test";
$result = mysql_query($query);
if (!$result) die ("Database access failed: " . mysql_error());
$rows = mysql_num_rows($result);
for ($j = 0 ; $j < $rows ; ++$j)
{
$row = mysql_fetch_row($result);
?>
<pre>
Line Number $row[0]
Product Name $row[1]
Quantity Available $row[2]
</pre>
<form action="index.php" method="post">
<input type="hidden" name="delete" value="yes" />
<input type="hidden" name="avail" value="$row[2]" />
<input type="submit" name="DELETE RECORD" /></form>
<?php
}
mysql_close($db_server);
function get_post($var)
{
return mysql_real_escape_string($_POST[$var]);
}
?>
you have error in the delete statment , try out this code :
<?php
require_once 'login.php';
// Create connection
$db_server = mysql_connect($db_hostname, $db_username, $db_password);
// Check connection
if (!$db_server) die("Unable to connect to MySQL: " . mysql_error());
mysql_select_db($db_database, $db_server)
or die("Unable to select database: " . mysql_error());
if (isset($_POST['delete']) && isset($_POST['avail']))
{
$avail = get_post('avail');
$query = "DELETE FROM test WHERE avail='$avail'";
if (!mysql_query($query, $db_server))
echo "DELETE failed: $query<br />" .
mysql_error() . "<br /><br />";
}
if (isset($_POST['id']) &&
isset($_POST['item_name']) &&
isset($_POST['avail']))
{
$id = get_post('id');
$item_name = get_post('item_name');
$avail = get_post('avail');
$query = "INSERT INTO test VALUES" .
"('$id','$item_name','$avail')";
if (!mysql_query($query, $db_server))
{echo "INSERT failed: $query<br />" .
mysql_error() . "<br /><br />";
}
}
?>
<form action="index.php" method="post"><pre>
Line Number <input type="text" name="id" />
Product Name <input type="text" name="item_name" />
Quantity Available <input type="text" name="avail" />
<input type="submit" value="ADD RECORD" />
</pre></form>
<?php
$query = "SELECT * FROM test";
$result = mysql_query($query);
if (!$result) die ("Database access failed: " . mysql_error());
$rows = mysql_num_rows($result);
for ($j = 0 ; $j < $rows ; ++$j)
{
$row = mysql_fetch_row($result);
?>
<pre>
Line Number $row[0]
Product Name $row[1]
Quantity Available $row[2]
</pre>
<form action="index.php" method="post">
<input type="hidden" name="delete" value="yes" />
<input type="hidden" name="avail" value="$row[2]" />
<input type="submit" name="DELETE RECORD" /></form>
<?php
}
mysql_close($db_server);
function get_post($var)
{
return mysql_real_escape_string($_POST[$var]);
}
?>
If no errors are being displayed add the following to the top of your file, it will allow for errors to be shown:
error_reporting(E_ALL);
ini_set('display_errors', '1');
Please ensure that you are displaying errors.
ini_set("display_errors", "1");
You can also create a new page with just
phpinfo();
to check that PHP is running (and what PHP configuration you have).

Array is returning empty

I want to retrieve the input value entered by the user and send it to the database. But for some reason it inserts a blank space in the table, instead of the value! Does someone know what's wrong?
<form method="post" name="name" action="pt2.php" >
<?php
//$mysql->commit();
echo "<h3>";
echo "Please enter the name for each seat:<br><p>&nbsp</p>";
echo "";
foreach($_POST['seats'] AS $seat) {
$rowId = substr($seat, 0, 1);
$columnId = substr($seat, 1);
echo $rowId . $columnId . '<input type="hidden" name="seats[]" value="' . $seat . '"><input name="' . $seat . 'name" type="text"/></br>';
}
?>
<input type="submit" name="submit" value="Submit Form"><br>
</form>
pt2.php
<?php
// Connect to MySQL
mysql_connect("localhost", "root", "root") or die("Connection Failed");
mysql_select_db("tickets")or die("Connection Failed");
$namei = $_POST[$seat . 'name'];
foreach ($_POST['seats'] as $seat){
echo $seat;
echo $namei;
$query = "INSERT INTO seatnames (seatname) VALUES ('$namei')";
mysql_query($query) or die(mysql_error());
}
?>

Unknown Column In Where Clause mysql query

My query is $query = "SELECT * FROM cartmatch WHERE CARTNO=$cart4"; and I'm receiving an error that says "Unknown column 'M833' in 'where clause'". Just so you know, cart4=M833.
::EDIT::
For some reason, nothing is showing. Here is the code on the page.
<?php
$cart1 = rawurldecode($_GET["path"]);
list( , , , , , $cart2) = explode ("\\", $cart1);
$cart3 = $cart2;
list($cart4) = explode (" ", $cart3);
$con = mysql_connect("SERVER","USERNAME","PASSWORD");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("cartmatch", $con);
$result = mysql_query("SELECT * FROM cartmatch WHERE CARTNO='$cart4'");
while($row = mysql_fetch_array($result))
{
echo '<form enctype="multipart/form-data" action="album.php" method="POST">Please enter press save.<br><br><input name="ID" type="hidden" value=';
echo $_GET["ID"];
echo ' ><input name="enabled" type="hidden" value=';
echo $_GET["enabled"];
echo ' ><input name="artist" type="hidden" value=';
echo $_GET["artist"];
echo ' ><input name="title" type="hidden" value="';
echo $_GET["title"];
echo '" >Name:<br/><input name="album" type="text" autofocus="autofocus" value="';
echo $row['ALBUM'];
echo '" ><input type="submit" name="edit" value="Save"></form>';
}
mysql_close($con);
?>
Change the query to:
"SELECT * FROM cartmatch WHERE CARTNO='$cart4'"
and change
list($cart4) = explode (" ", $cart3);
to
list($cart4) = explode ("+", $cart3);
Change the WHERE section to
CARTNO='$cart4'

Categories