A php/mySQL booking function that's been working well suddenly stopped inserting booking entries into the database, with no changes to the code and a functioning database connection.
I run a parallel version of the page that is working on another website; the only difference between the two is that the broken version is running on php 5.6, the functioning one is still on 5.4.
Adding an error log brings no results even though the table doesn't update and I can't see any deprecated statements between php 5.4 and 5.6.
Can anyone spot the problem I'm missing?
//If the confirm button has been hit:
if (isset($_POST['submit'])) {
//Create the foreach loop
foreach ($_POST['class_id'] as $classes) {
$class_id = (int)$classes;
//UPDATE the bookings table **THIS PART IS NOT WORKING**:
$query = "INSERT INTO bookings (user_id, booking_name, class_id, time_stamp) VALUES ('$user_id', '$username', '$class_id', NOW())";
mysqli_query($dbc, $query);
}
foreach($_POST['class_id'] as $classes){
$class_id = (int)$classes;
//Change the booking numbers **THIS WORKS FINE**:
$increase = "UPDATE classes SET online_bookings = (online_bookings + 1), total_bookings = (total_bookings + 1), free_spaces = (free_spaces - 1) WHERE class_id = $class_id";
mysqli_query($dbc, $increase);
}
mysqli_close($dbc);
..and the table that provides the $_POST data:
echo'<div class="container">';
echo'<div class="span8 offset1 well">';
echo'<p class="lead text-info">Do you want to reserve space at these classes?</p>';
//table header
echo '<table id="dancers" class="table table-bordered table-hover">';
echo '<thead><tr><th>Date</th><th>Time</th><th>Venue</th><th>Who\'s going?</th></tr></thead>';
//create the form
echo '<form id="makebkg" method="post" action="' . $_SERVER['PHP_SELF'] . '">';
//Get the class IDs from the GET to use in the POST
foreach ($_GET['sesh'] as $class_id) {
$sql = "SELECT class_id, DATE_FORMAT(date, '%a, %d %b') AS new_date, DATE_FORMAT(time, '%H:%i') AS new_time, venue FROM classes WHERE class_id = '$class_id'";
$data = mysqli_query($dbc, $sql);
//get table data
while ($row = mysqli_fetch_array($data)) {
$date = $row["new_date"];
$time = $row["new_time"];
$venue = $row["venue"];
$class_id = $row["class_id"];
}
//Show a table of the selected classes
echo '<tr><td>' . $date . '</td>';
echo '<td>' . $time . '</td>';
echo '<td>' . $venue . '</td>';
echo '<td>' . $username . '</td></tr>';
echo '<input type="hidden" name="date[]" value="' . $date . '" />';
echo '<input type="hidden" name="time[]" value="' . $time . '" />';
echo '<input type="hidden" name="venue[]" value="' . $venue. '" />';
echo '<input type="hidden" name="username[]" value="' . $username . '" />';
echo '<input type="hidden" name="class_id[]" value="' . $class_id . '" />';
}
echo'</table>';
//Go Back button
echo '<a class="btn btn-link pull-left" href="classes.php"><i class="icon-arrow-left"></i> Go back</a>';
// Make booking button - LIVE
echo'<div id="confirmbtn">';
echo '<input type="submit" id="confirm" name="submit" class="btn btn-large btn-primary pull-right" value="Confirm">';
echo '</div>';
OK, I finally fixed the problem.
It turns out that the hosting company had changed the MySQL mode to 'strict'.
The INSERT statement here left some table columns blank and strict mode rejects the entire insert as a result. Changing the mode right before the insert command was a quicker way to get around the problem than updating the insert command:
// TURN OFF STRICT MYSQL MODE
$strict = "SET sql_mode = ''";
mysqli_query($dbc, $strict);
Thanks for all the advice and tolerance of an indolent coder.
Did you try to check your query?
error_reporting(1);
$q = mysqli_query($dbc, $query);
if (!$q)
{
echo 'Error' . mysqli_error($dbc);
}
Do same for other query.
Related
I have a php page that interacts with a database. I am trying to display data from the database as options for a user to select. I am trying to record which option a user selects but the variable I use ($a_game_id) to record which button is clicked gets reverted back to its original value after submitting another form. I have tried declaring the variable as global within the loop and using session variables.
$a_game_id = 9;//starting value - it changes from 9 as desired, but reverts back when another form is submitted
$sql = "SELECT * FROM nbagames WHERE date = '" .$date ."'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
global $a_game_id;
// output data of each row
$results = $conn->query($sql);
$resultset = array();
while($a_row = $results->fetch_assoc()){
$resultset[] = $a_row;
}
foreach ($resultset as $row){
echo "<form action='display_lines.php' method='POST'>
<br>" . $row["away"] . " " . $row['away_spread'] . "---TOTAL AVAILABLE: " . $away_sum_array[$row['game_id']].
" <input type='submit' value='Bet " . $row['away'] ."' name='" . $row['game_id']."A' />
at " . $row["home"] . " " . $row['home_spread'] . "---TOTAL AVAILABLE: " .$home_sum_array[$row['game_id']]. "
<input type='submit' value= 'Bet " . $row['home'] ."' name='" . $row['game_id']."H' /> " . $row['date'] . "
</form>
<br>";
///HERE $a_game_id has gets the desired value
if(isset($_POST[$row['game_id'].'H'])){
$a_game_id = intval($row['game_id']);
}else if(isset($_POST[$row['game_id'].'A'])){
$a_game_id = intval($row['game_id']);
}
}
} else {
echo "<br> 0 results";
}
$sql = "SELECT * FROM nbagames WHERE date = '" .$date ."'";
$result = $conn->query($sql);
while($row = $result->fetch_assoc()) {
if(isset($_POST[strval($row['game_id']).'H'])){
echo '<h3>'.$row['game_id'].'<br>';
echo $row['home'].' '.$row['home_spread'].'<br>';
$team = $row['home'];
$team_spread = $row['away_spread'];
echo '<form action="display_lines.php" method="post">
<input type="text" name="new_bet_amount" placeholder="Enter Bet Amount">
<input type="submit" name="new_bet_submit" value="Submit Bet">
</form></h3>';
}
else if(isset($_POST[strval($row['game_id']).'A'])){
echo '<h3>' .$row['game_id'].'<br>';
echo $row['away'].' '.$row['away_spread'].'<br>';
$team = $row['away'];
$team_spread = $row['away_spread'];
echo '<form action="display_lines.php" method="post">
<input type="text" name="new_bet_amount" placeholder="Enter Bet Amount">
<input type="submit" name="new_bet_submit" value="Submit Bet">
</form></h3>';
}
}
if(isset($_POST['new_bet_submit'])){
//HERE $a_game_id reverts back to its original value which is undesirable
$sql3 = "INSERT INTO placed_bets (user_id, game_id, bet_amount, game_date) VALUES ('".$_SESSION['id']."', '".$a_game_id."', '".$_POST['new_bet_amount']."', '".$date."')";
echo $a_game_id.'<br>';
if ($conn->query($sql3) === TRUE) {
echo "<br><h3>BET PLACED SUCCESSFULLY</h3><br>";
} else {
echo '<h3>Error placing bet<br>';
echo $conn->error;
echo '</h3>';
}
}
Thank you for taking a look
Do you mean "when I make another request all my global variables get reset?" If so, yes, that's how they work. Each request is completely independent of others. They do not share variables or other data. All you get is what's in $_SESSION, $_GET, $_POST and $_COOKIE.
If you need to persist between requests you must put that in the session, the database, or something persistent.
If you're used to code where the process persists and the variables stick, like in client-side situations, that's a mode of thinking you need to completely abandon.
I have a script on my website which handles a notification system for messages, and alerts. This script pulls in the data from a MySQL Database, and it works like a charm locally, but on my live site, the query fails. This script always returns false and goes to the else condition live, but locally it works exactly the way it should.
$queryNots = 'Select message, DATE_FORMAT(timeSent, "%h:%i %p %W, %M %D ") timeSent FROM notifications WHERE UserID="' . $userId . '" AND seen="n";';
if ($result = $con->query ($query)) {
$resultNum = mysqli_num_rows ( $result );
echo '<h2>You have ' . $resultNum . ' Notifications</h2>
<ul>';
while ( $row = $result->fetch_assoc () ) {
echo '<li>' . $row ["message"] . ' # ' . $row ['timeSent'] . '</li>';
}
if ($resultNum > 0) {
echo '
</ul>
<form method="post" action="deleteNots.php">
<input type="hidden" name="curID" value="' . $userId . '">
<input type="submit" value="Dismiss">
</form>
';
}
} else {
echo '<h2>Error reading table</h2>';
}
On the live site if I take out the if-else and just run the query not only does it fail, it kills the entire script. I have no idea what could be causing this, I know the tables are the same, because I use them in a different script just fine.
I have absolutely nothing to work with, because it isn't giving me any sort of MySQL error message to go off of. I really want to get this figured out because I want to have this site back working as soon as possible.
$query = 'Select message, DATE_FORMAT(timeSent, "%h:%i %p %W, %M %D ") timeSent FROM notifications WHERE UserID="' . $userId . '" AND seen="n"';
$result = $con->query($query);
if ($result) {
$resultNum = mysqli_num_rows($result);
echo '<h2>You have '.$resultNum.'Notifications</h2><ul>';
while($row = $result->fetch_assoc()) {
echo '<li>' . $row ["message"] . ' # ' . $row ['timeSent'] . '</li>';
}
if ($resultNum > 0) {
echo '
</ul>
<form method="post" action="deleteNots.php">
<input type="hidden" name="curID" value="' . $userId . '">
<input type="submit" value="Dismiss">
</form>
';
}
} else {
echo '<h2>Error reading table</h2>';
}
NOTE:
Change the $queryNots variable because you used the $query variable below code
i.e: $result = $con->query($query);
unwanted semicolon(;) in $queryNots :: seen="n";';
The solution to my issue ended up coming in the comments of the question, so I couldn't mark it as completed.
try echo mysql_error() to see what´s going on there – handsome Jul 29
'17 at 14:51
So in this part of the website i'm making an edit person information page and if there are more than one person with the name searched you get a table with all the persons; you choose the needed one and edit it in another page.
I need to pass the array that matches with the person selected. I don't know how to pick the array in the other page through POST. This is a part of code of the page that sends the array:
$squery=mysqli_query($conn,"SELECT * FROM amico WHERE (Nome= '" .
$nome . "') AND (Cognome ='" .$cognome. "')");
$num = mysqli_num_rows($squery);
$i=0;
$array= array();
while($rowa=mysqli_fetch_assoc($squery)){
$array[$i]=$rowa;
$i++;
}
$ssquery=mysqli_query($conn,"SELECT * FROM amico WHERE (Nome= '" .
$nome . "') AND (Cognome ='" .$cognome. "')");
if($num > 1) {
echo 'Trovato più di un elemento';
echo '<table border="1">';
echo '<tr>';
echo '<td><p>S</p></td>';
echo '<td><p>Nome</p></td>';
echo '<td><p>Cognome</p></td>';
echo '<td><p>Città</p></td>';
echo '</tr>';
$i=0;
echo '<form method="POST" action="moficatr.php">';
while ($row = mysqli_fetch_array($ssquery)) {
echo '<tr>';
echo '<td> <p> <input type="Radio" name="persona" value="'.
$rowa[$i] . '"></p></td>';
echo ' <td><p>' .$row['Nome'] . '</p></td>';
echo '<td><p>'.$row['Cognome'].'</p></td>';
echo '<td><p>'.$row['Citta'].'</p></td>';
echo '</tr>';
$i++;
}
echo '</table>';
echo '<br><br><input type="submit" value="Modifica"></form>';
}
You can simply pass the selected user's ID to the next page and then fetch all the details using the ID from that page instead of sending the whole array of data to the other side.
Why are you executing the same query twice in your code ? you only need one query which will give you all the data.
Also I strongly encourage you to use PDO please - http://php.net/manual/en/book.pdo.php Or at least prepared statments which is supported in MySQLi as well
$ssquery=mysqli_query($conn,"SELECT * FROM amico WHERE (Nome= '" .
$nome . "') AND (Cognome ='" .$cognome. "')");
$num = mysqli_num_rows($ssquery);
if($num > 1) {
echo 'Trovato più di un elemento';
echo '<table border="1">';
echo '<tr>';
echo '<td><p>S</p></td>';
echo '<td><p>Nome</p></td>';
echo '<td><p>Cognome</p></td>';
echo '<td><p>Città</p></td>';
echo '</tr>';
echo '<form method="POST" action="moficatr.php">';
while ($row = mysqli_fetch_assoc($ssquery)) {
echo '<tr>';
echo '<td> <p> <input type="Radio" name="persona" value="'.
$row['id'] . '"></p></td>'; //you can change 'id' with your column name if it's different
echo ' <td><p>' .$row['Nome'] . '</p></td>';
echo '<td><p>'.$row['Cognome'].'</p></td>';
echo '<td><p>'.$row['Citta'].'</p></td>';
echo '</tr>';
$i++;
}
moficatr.php
//Set up a Mysql connection.
$user_id = (int) $_POST['persona'];
//Query the db to fetch all the details of this user.
$query = "SELECT * from your_table where id=?";
//Prepare statment and execute.
$stmt = mysqli_prepare($connection, $query);
mysqli_stmt_bind_param($stmt, "i", $user_id);
mysqli_stmt_execute($stmt);
/* bind result variables */
mysqli_stmt_bind_result($stmt, $name, $address,...);
echo $name;
echo $address;
I'm using checkbox buttons for updating occupation of day in month in my calendar.
If I check just one checkbox it updates it, but if I check multiple checkboxes it doesn't update any row.
Here is my code:
$query = mysqli_query($con, "SELECT * FROM koledar");
while ($vsidnevi = mysqli_fetch_assoc($query)) {
$dnevi = [$vsidnevi['dan']];
foreach ($dnevi as $dan) {
if ($vsidnevi['zasedenost']==0) {
echo '<label class="btn btn-primary">';
echo '<input type="checkbox" name="miha" value="' . $dan . '" data-toggle="button">' . $dan;
echo '</label>';
} elseif ($vsidnevi['zasedenost']==1) {
echo '<label class="btn btn-primary active disabled">';
echo '<input type="checkbox" name="miha" value="' . $dan . '" data-toggle="button">' . $dan;
echo '</label>';
}
}
}
and
if (isset($_GET['dodaj']) && $_GET['dodaj']=="true") {
if(isset($_POST['miha'])) {
$daen = $_POST['miha'];
$dodaj = mysqli_query($con, "UPDATE koledar SET zasedenost=1 WHERE dan=" . $daen . "");
}
}
Firstly, you should be passing your literal values to MySQL as parameters to a prepared statement (in order to defeat SQL injection attacks).
When multiple values are submitted, $_POST['miha'] will be an array over which you must loop:
if (isset($_GET['dodaj']) && $_GET['dodaj']=="true") {
if(isset($_POST['miha'])) {
$dodaj = mysqli_prepare($con, '
UPDATE koledar
SET zasedenost=1
WHERE dan=?
');
mysqli_stmt_bind_param($dodaj, 's', $daen);
foreach ($_POST['miha'] as $daen) {
mysqli_stmt_execute($dodaj);
}
}
}
Or else use IN ():
if (isset($_GET['dodaj']) && $_GET['dodaj']=="true") {
if(isset($_POST['miha'])) {
$inq = implode(',', array_fill(0, count($_POST['miha']), '?'));
$dodaj = mysqli_prepare($con, "
UPDATE koledar
SET zasedenost=1
WHERE dan IN ($inq)
");
call_user_func_array('mysqli_stmt_bind_param', array_merge(
array(
$dodaj,
str_repeat('s', count($_POST['miha']))
),
$_POST['miha']
));
mysqli_stmt_execute($dodaj);
}
}
Dear friends i am not an expert in php and need your help to solve an issue.
I am trying to create a page where i can call data from MySql and can edit/update it. The first part to display the data is done but i am unable to update it ... friends kindly help me solve this.
function Get_pages($mysql) {
$PageQuery = $mysql->query("SELECT * FROM pages WHERE PageID = '$pageID'");
while (($row = $PageQuery->fetch_assoc()) !== null)
{
echo '<form action="page.php" method="post">';
echo '<span class="lbl">Page Title</span>';
echo '<input name="PageTitle" type="text" value="' . $row["PageTitle"] . '" />';
echo '<span class="lbl">Page Content</span>';
echo '<textarea class="txt-area" name="PageContent" cols="" rows="18">' . $row["PageContent"] . '</textarea>';
echo '<input name="UpdateBtn" value="Update Page" type="submit" class="submit_btn"></form>';
}
// WHEN BUTTON CLICKED
if ($_REQUEST['UpdateBtn'])
{
$pageID = $_REQUEST["$pageID"];
$PageTitle = addslashes($_REQUEST['PageTitle']);
$PageContent = addslashes($_REQUEST['PageContent']);
$sql = mysql_query ("UPDATE pages SET PageTitle='$PageTitle', PageContent='$PageContent' WHERE pageID='$pageID'") or die ("Not Updating");
}
}
$sql = mysql_query ("UPDATE
should be
$sql = $mysql->query("UPDATE
You are making connection with mysqli_* function and using mysql_* function for update , because of that your UPDATE is failing.