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'
Related
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>';
I have problem write back to table with UPDATE from mysql_fetch_assoc. I got it to work with INSERT but then it add a new row.
Is there anybody who can help me with the correct syntax?
I heve this query:
$sql = "SELECT * FROM oppgave WHERE modulid=5 AND resultat is NULL ORDER BY RAND() LIMIT 1";
$data = null;
$dataid = null;
$result = mysql_query($sql, $tilkobling);
echo "<hr>";
while ($nextrow= mysql_fetch_assoc($result)){
echo "Svar: " . $nextrow['besvarelse'];
echo "<br>Modulid: " . $nextrow['modulid'];
echo "<br>student: " . $nextrow['studentid'];
echo "<br>";
$data = $nextrow['modulid'];
$dataid = $nextrow['id'];
}
echo '<form name="input" action="tilretting.php" method="post">';
echo'Retter<input type="text" name="correctedby" value="'.$_SESSION['myusername'].'">';
echo '<input type="hidden" name="resultat" value="0">';
echo 'Godkjent<input type="checkbox" name="resultat" value="1">';
echo 'modul<input type="text" name="modulid" value="'.$data.'">';
echo 'id<input type="text" name="id" value="'.$dataid.'">';
echo '<input class="levermodulknapp" type="submit" name="lever1" value="Send inn retting">';
echo "</form>";
echo "<hr>";
?>
I And this is that i tryed put in the php who is submitted:
<?php
include "header.inc.php";
//in this file the connetion to server
include "funksjoner.inc.php";
$correctedby= $_POST['correctedby'];
$resultat= $_POST['resultat'];
$data = $nextrow['modulid'];
$dataid = $nextrow['id'];
//Step 2: connetion to db
$tilkobling = kobleTil(); //trenger ikke oppgi databasenavn
//Steg 3: Kjør en SQL-query
$sql = "UPDATE oppgave SET correctedby='".$correctedby."', resultat='".$resultat.", WHERE id='".$dataid."'";
mysql_query($sql, $tilkobling);
?>
What am i doing wrong?
So grateful for any tip on this one!
You are doing unnneccessary concatenations and there is an extra comma before the WHERE clause..
The right way..
$sql = "UPDATE oppgave SET correctedby='$correctedby', resultat='$resultat' WHERE id='$dataid'";
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!
I've been looking through many threads on here without finding a solution to my problem.
I've created a form that is supposed to show content of a database in input boxes, and when i change the content, it should be updated in the database.
No errors, nothing gets changed.
<?php
$con=mysqli_connect("localhost","root","","frontpage");
// Check connection
if (mysqli_connect_errno()){
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM frontpage_left_links")
or die("Error: ".mysqli_error($con));
while($row = mysqli_fetch_array($result)){
echo '<form action="" method="post">';
echo '<div style="float:left">';
echo '<table border="1" bordercolor="#000000">';
echo '<tr>';
echo '<td>link</td>';
echo '<td><input type="text" name="linkid" value="'.$row['link'].'"></td>';
echo '</tr>';
echo '<tr>';
echo '<td>img</td>';
echo '<td><input type="text" name="imgid" value="'.$row['img'].'"></td>';
echo '</tr>';
echo '<tr>';
echo '<td>tekst</td>';
echo '<td><input type="text" name="imgid" value="'.$row['name'].'"></td>';
echo '</tr>';
echo '<tr>';
echo '<td><input type="submit" id="update" name="gem" value="Gem"</td></td>';
echo '<td><input type="hidden" name="id" value="'.$row['id'].'"></td>';
echo '</tr>';
echo '</table></div>';
echo '<div style="float:left"><center><img src="img/'.$row['img'].'"><br />'.$row['name'].'</center></div>';
echo '</form><br /><br /><br /><br /><br /><br /><br /><br />';
}
if(isset($_POST['update'])){
$id = $_POST['id'];
$link = $_POST['linkid'];
$img = $_POST['imgid'];
$name = $_POST['nameid'];
$sql = mysqli_query("UPDATE frontpage_left_links SET link = '$link', img = '$img', name = '$name' WHERE id = '$id'");
$retval = mysqli_query( $sql, $con );
if(! $retval ){
die('Could not update data: ' . mysql_error());
}
echo "Updated data successfully\n";
}
mysqli_close($con);
?>
The form show the database content fine, but nothing happens when changed.
I appreciate any help I can get.
This is what it looks like now.
<?php
$con=mysqli_connect("localhost","root","","frontpage");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
if(isset($_POST['gem']))
{
$id = $_POST['id'];
$link = $_POST['linkid'];
$img = $_POST['imgid'];
$name = $_POST['nameid'];
$sql = mysqli_query("UPDATE frontpage_left_links SET link = '$link', img = '$img', name = '$name' WHERE id = '$id'");
$retval = mysqli_query( $con, $sql );
if(! $retval )
{
die('Could not update data: ' . mysql_error());
}
echo "Updated data successfully\n";
}
$result = mysqli_query($con,"SELECT * FROM frontpage_left_links")
or die("Error: ".mysqli_error($con));
while($row = mysqli_fetch_array($result))
{
echo '<form action="" method="post">';
echo '<div style="float:left">';
echo '<table border="1" bordercolor="#000000">';
echo '<tr>';
echo '<td>link</td>';
echo '<td><input type="text" name="linkid" value="'.$row['link'].'"></td>';
echo '</tr>';
echo '<tr>';
echo '<td>img</td>';
echo '<td><input type="text" name="imgid" value="'.$row['img'].'"></td>';
echo '</tr>';
echo '<tr>';
echo '<td>tekst</td>';
echo '<td><input type="text" name="nameid" value="'.$row['name'].'"></td>';
echo '</tr>';
echo '<tr>';
echo '<td><input type="submit" id="update" name="gem" value="Gem"</td></td>';
echo '<td><input type="hidden" name="id" value="'.$row['id'].'"></td>';
echo '</tr>';
echo '</table></div>';
echo '<div style="float:left"><center><img src="img/'.$row['img'].'"><br />'.$row['name'].'</center></div>';
echo '</form><br /><br /><br /><br /><br /><br /><br /><br />';
}
mysqli_close($con);
?>
Now i get this error.
Warning: mysqli_query() expects at least 2 parameters, 1 given in /Applications/XAMPP/xamppfiles/htdocs/page/admin.php on line 17
Warning: mysqli_query(): Empty query in /Applications/XAMPP/xamppfiles/htdocs/page/admin.php on line 19
Could not update data:
Because your udate is at the end of the page put it above the rest.
And also change isset($_POST['update'] to isset($_POST['gem']
<?php
$con=mysqli_connect("localhost","root","","frontpage");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
if(isset($_POST['gem']))
{
$id = $_POST['id'];
$link = $_POST['linkid'];
$img = $_POST['imgid'];
$name = $_POST['nameid'];
$sql = "UPDATE frontpage_left_links SET link = '$link', img = '$img', name = '$name' WHERE id = '$id'";
$retval = mysqli_query($con,$sql );
if(! $retval )
{
die('Could not update data: ' . mysql_error());
}
echo "Updated data successfully\n";
}
$result = mysqli_query($con,"SELECT * FROM frontpage_left_links")
or die("Error: ".mysqli_error($con));
while($row = mysqli_fetch_array($result))
{
echo '<form action="" method="post">';
echo '<div style="float:left">';
echo '<table border="1" bordercolor="#000000">';
echo '<tr>';
echo '<td>link</td>';
echo '<td><input type="text" name="linkid" value="'.$row['link'].'"></td>';
echo '</tr>';
echo '<tr>';
echo '<td>img</td>';
echo '<td><input type="text" name="imgid" value="'.$row['img'].'"></td>';
echo '</tr>';
echo '<tr>';
echo '<td>tekst</td>';
echo '<td><input type="text" name="imgid" value="'.$row['name'].'"></td>';
echo '</tr>';
echo '<tr>';
echo '<td><input type="submit" id="update" name="gem" value="Gem"</td></td>';
echo '<td><input type="hidden" name="id" value="'.$row['id'].'"></td>';
echo '</tr>';
echo '</table></div>';
echo '<div style="float:left"><center><img src="img/'.$row['img'].'"><br />'.$row['name'].'</center></div>';
echo '</form><br /><br /><br /><br /><br /><br /><br /><br />';
}
mysqli_close($con);
?>
try to replace :
echo '<td><input type="submit" id="update" name="gem" value="Gem"</td></td>';
with :
echo '<td><input type="submit" id="update" name="update" value="Gem"</td></td>';
In your form you are having update button name as gem but you are using if(isset($_POST['update'])) to run update query.
Change it to if(isset($_POST['gem']))
The easiest way would be to simply change the name attribute on your submit to the $_POST[] variable you used. name="update"The easiest way would be to simply change the name attribute on your submit to the $_POST[] variable you used. name="update"
Edit :: Answered already.
echo '<td><input type="submit" id="update" name="update" value="Gem"</td></td>';
Just delete the second td from above!!!
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);