HTML/PHP Form Submitting to 404 Eorror - php

SO. I work on this Wordpress site. Which believe me is SO annoying. So I have this huge mass of code which is a combo of PHP and HTML. Everything appears just fine until the form is submitted. When the page refreshes it loads to the same page (with the same exact URL) but with a 404 Error.
<center>
<code>
[insert_php]
// Info to connect to the Wishlist database
$servername = "eggcavity.com";
$dbusername = "******";
$password = "*******";
$dbname = "EggcavityTravelIndex";
try {
// To connect to the database please
$conn = new mysqli($servername, $dbusername, $password, $dbname);
if ($conn->connect_error) {
die('Connect Error (' . $conn->connect_errno . ') ' . $conn->connect_error);
}
// Get current user's username
$current_user = wp_get_current_user();
$username = $current_user->user_login;
$allowed_roles = array('moderator', 'administrator');
if ($username == "") {
// If person is not logged in
echo "Please login to see if you have moderator privileges.";
} else if (array_intersect($allowed_roles, $current_user->roles )) {
// If person is logged in and has moderator privileges, show the page
$page = 1;
if($_SERVER['QUERY_STRING'] != ""){
$page = $_SERVER['QUERY_STRING'];
}
if(isset($_POST['submit'])){
$ids = $_POST['ids'];
$theCount = 0;
// Prepare and bind the udpate statement
$sql2 = "UPDATE Travels SET Name = ?, Picture = ?, Price = ?, " .
"Description = ?, Rarity = ?, Status = ? WHERE ID = ?";
$stmt2 = $conn->prepare($sql2);
$stmt2->bind_param('ssssssi', $name, $picture, $price, $description, $rarity, $status, $id);
foreach($ids as $id){
$namecode = $id . "name";
$picturecode = $id . "picture";
$pricecode = $id . "price";
$raritycode = $id . "rarity";
$descriptioncode = $id . "description";
$statuscode = $id . "status";
$name = $_POST[$namecode];
$picture = $_POST[$picturecode];
$price = $_POST[$pricecode];
$rarity = $_POST[$raritycode];
$description = $_POST[$descriptioncode];
$status = $_POST[$statuscode];
$stmt2->execute();
}
if(isset($_POST['delete'])) {
$delete=!empty( $_POST['delete'] ) ? $_POST['delete'] : false;
if($delete && !is_array($delete) ){
$delete=explode(',',$delete);
}
foreach($delete as $id){
$sql = "DELETE FROM Travels WHERE ID = ?";
$stmt = $conn->prepare($sql);
$stmt->bind_param('i', $id);
$stmt->execute();
$theCount++;
}
}
echo "<font size='3'>The travel database has been updated, and " . $theCount .
" travels(s) has/have been removed.";
} else {
// Just display the form
$start = 0;
$stop = 100;
if ($page == "2") {
$start = 100;
$stop = 100;
} else if ($page == "3") {
$start = 200;
$stop = 100;
} else if ($page == "4") {
$start = 300;
$stop = 100;
} else if ($page == "5") {
$start = 400;
$stop = 100;
} else if ($page == "6") {
$start = 500;
$stop = 100;
} else if ($page == "7") {
$start = 600;
$stop = 100;
} else if ($page == "8") {
$start = 700;
$stop = 100;
} else if ($page == "9") {
$start = 800;
$stop = 100;
} else if ($page == "10") {
$start = 900;
$stop = 100;
} else if ($page == "11") {
$start = 1000;
$stop = 100;
} else if ($page == "12") {
$start = 1100;
$stop = 100;
} else if ($page == "13") {
$start = 1200;
$stop = 100;
} else if ($page == "14") {
$start = 1300;
$stop = 100;
} else if ($page == "15") {
$start = 1400;
$stop = 100;
}
$sql = 'SELECT * FROM Travels LIMIT ' . $start . ',' . $stop;
$result = mysqli_query($conn, $sql);
$sql11 = 'SELECT * FROM Travels';
$result11 = mysqli_query($conn, $sql11);
$numtravels = mysqli_num_rows($result11);
if ($numtravels > 0) {
$numpages = ceil($numtravels / 100);
$cnt = 1;
echo 'Pages: ';
while ($cnt <= $numpages){
echo '<a style="border-style:solid;border-radius:5px;border-width:1px;padding:2px;" href="?' . $cnt . '">'
. $cnt . '</a> ';
$cnt++;
}
echo '<br><br>';
$begin = $start + 1;
$end = $begin + 99;
echo '<form method="POST"><table style="border-collapse: collapse";>' .
'Travels ' . $begin . ' to ' . $end;
while($row = $result->fetch_assoc()) {
$name = $row["Name"];
$id = $row["ID"];
$picture = $row["Picture"];
$price = $row["Price"];
$rarity= $row["Rarity"];
$description = $row["Description"];
$status= $row["Status"];
echo '<tr>';
echo '<td rowspan="2" style="border:1px solid gray;text-align:center;"><br><img src="' . $picture . '"></td>';
echo '<td style="border:1px solid gray;text-align:center;">' .
'<strong>Name: </strong><input type="text" name="' . $id . 'name" value="' . $name . '"></td>';
echo '<td style="border:1px solid gray;text-align:center;">' .
'<strong>Image URL: </strong><input type="text" name="' . $id . 'picture" value="' . $picture . '"></td>';
echo '<td style="border:1px solid gray;text-align:center;">' .
'<strong>Price: </strong><input type="text" name="' . $id . 'price" value="' . $price . '"></td>';
echo '<td style="border:1px solid gray;text-align:center;">' .
'<strong>Rarity: </strong><input type="text" name="' . $id . 'rarity" value="' . $rarity . '"></td>';
echo '</tr>';
echo '<td colspan="2" style="border:1px solid gray;text-align:center;">' .
'<strong>Description: </strong><br><input style="width:300px" type="text" name="' . $id . 'description" value="' . $description . '"></td>';
echo '<td style="border:1px solid gray;text-align:center;"><strong>Status: </strong><br><select name="' . $id . 'status">' .
'<option value ="' . $status . '" selected>' . $status . '</option>';
if ($status == "Available") {
echo '<option value ="Retired">Retired</option>';
} else if ($status == "Retired") {
echo '<option value ="Available">Available</option>';
} else if ($status == "") {
echo '<option value ="Retired">Retired</option>';
echo '<option value ="Available">Available</option>';
}
echo '</select></td>';
echo '<td style="border:1px solid gray;text-align:center;">' . '<strong>Delete</strong><br>' .
'<input type="checkbox" name="delete[]" value="' . $id . '"></td>';
echo '</tr>';
}
echo '</table><br><input name="submit" type="submit" id="submit" value="Update">' .
'</form><br><br>';
} else {
echo "<br>There are no travels in the database.";
}
}
} else {
// If person is logged in but does not have mod privileges
echo "Sorry, you're not a moderator!";
}
} catch (mysqli_sql_exception $e) {
throw $e;
}
// Close the connection to the database
$conn->close();
[/insert_php]
</code>
</center>
This is the form
And this is the page after submission.
The code is SO similar to a page that I do have working. As shown here:
<center>
<code>
[insert_php]
// Info to connect to the Wishlist database
$servername = "eggcavity.com";
$dbusername = "********";
$password = "*******";
$dbname = "EggcavityTravelIndex";
try {
// To connect to the database please
$conn = new mysqli($servername, $dbusername, $password, $dbname);
if ($conn->connect_error) {
die('Connect Error (' . $conn->connect_errno . ') ' . $conn->connect_error);
}
// Get current user's username
$current_user = wp_get_current_user();
$username = $current_user->user_login;
if ($username == ""){
echo "Please login to use wishlist feature.";
} else {
echo "<font size ='3'><br>Please click <strong>" .
"<a href = 'http://eggcavity.com/add-wishlist'>here</a></strong>" .
" to add creatures to your wishlist.<br><br>Please click <strong>" .
"<a href='http://eggcavity.com/wishlists'>here</a></strong> to view your wishlist.<br>" .
"<br>At the moment when you enter notes you cannot view them on the edit page." .
"<br>Please go to the view wishlist page to see your notes.<br><br></font>";
$page = 1;
if($_SERVER['QUERY_STRING'] != ""){
$page = $_SERVER['QUERY_STRING'];
}
if(isset($_POST['submit'])){
$ids = $_POST['ids'];
$theCount = 0;
// Prepare and bind the udpate statement
$sql2 = "UPDATE Wishlists SET Picture = ?, Stage = ?, Gender = ?, " .
"Frozen = ?, Notes= ? WHERE ID = ?";
$stmt2 = $conn->prepare($sql2);
$stmt2->bind_param('sssssi', $picture, $stage, $gender, $frozen, $notes, $id);
foreach($ids as $id){
$stagecode = $id . "stage";
$gendercode = $id . "gender";
$frozencode = $id . "frozen";
$notescode = $id . "notes";
$namecode = $id . "creature";
$stage = $_POST[$stagecode];
$Stage = $stage;
$gender = $_POST[$gendercode];
$frozen = $_POST[$frozencode];
$notes = $_POST[$notescode];
$name = $_POST[$namecode];
$sql1 = 'SELECT * FROM Creatures WHERE Name = "' . $name . '"';
$result = mysqli_query($conn, $sql1);
$row = $result->fetch_assoc();
$picture = $row["$stage"];
$stmt2->execute();
}
if(isset($_POST['delete'])) {
$delete=!empty( $_POST['delete'] ) ? $_POST['delete'] : false;
if($delete && !is_array($delete) ){
$delete=explode(',',$delete);
}
foreach($delete as $id){
$sql = "DELETE FROM Wishlists WHERE ID = ?";
$stmt = $conn->prepare($sql);
$stmt->bind_param('i', $id);
$stmt->execute();
$theCount++;
}
}
echo "<font size='3'>Your wishlist has been updated, and " . $theCount .
" creature(s) has/have been removed from your wishlist.<br><br>Please click <u>" .
"<strong><a href='http://eggcavity.com/edit-wishlist'>here</a></u></strong>" .
" to return to the edit page.</href><br><br>";
} else {
// Just display the form
$start = 0;
$stop = 100;
if ($page == "2") {
$start = 100;
$stop = 100;
} else if ($page == "3") {
$start = 200;
$stop = 100;
} else if ($page == "4") {
$start = 300;
$stop = 100;
} else if ($page == "5") {
$start = 400;
$stop = 100;
} else if ($page == "6") {
$start = 500;
$stop = 100;
} else if ($page == "7") {
$start = 600;
$stop = 100;
} else if ($page == "8") {
$start = 700;
$stop = 100;
} else if ($page == "9") {
$start = 800;
$stop = 100;
} else if ($page == "10") {
$start = 900;
$stop = 100;
} else if ($page == "11") {
$start = 1000;
$stop = 100;
} else if ($page == "12") {
$start = 1100;
$stop = 100;
} else if ($page == "13") {
$start = 1200;
$stop = 100;
} else if ($page == "14") {
$start = 1300;
$stop = 100;
} else if ($page == "15") {
$start = 1400;
$stop = 100;
}
$sql = 'SELECT * FROM Wishlists WHERE Username = "' . $username .
'" ORDER BY Creature LIMIT ' . $start . ',' . $stop;
$result = mysqli_query($conn, $sql);
$sql11 = 'SELECT * FROM Wishlists WHERE Username = "' . $username . '"';
$result11 = mysqli_query($conn, $sql11);
$numcreatures = mysqli_num_rows($result11);
if ($numcreatures > 0) {
$numpages = ceil($numcreatures / 100);
$cnt = 1;
echo 'Pages: ';
while ($cnt <= $numpages){
echo '<a style="border-style:solid;border-radius:5px;border-width:1px;padding:2px;" href="?' . $cnt . '">'
. $cnt . '</a> ';
$cnt++;
}
echo '<br><br>';
$begin = $start + 1;
$end = $begin + 99;
echo '<form method="POST"><table style="border-collapse: collapse";><strong>' .
'Creatures ' . $begin . ' to ' . $end .
'<tr>' .
'<td>' . $numcreatures . ' creatures</td>' .
'<td style="border:1px solid gray;text-align:center;">Creature</td>' .
'<td style="border:1px solid gray;text-align:center;">Stage</td>' .
'<td style="border:1px solid gray;text-align:center;">Gender</td>' .
'<td style="border:1px solid gray;text-align:center;">Frozen</td>' .
'</tr></strong>';
while($row = $result->fetch_assoc()) {
$creature = $row["Creature"];
$id = $row["ID"];
$picture = $row["Picture"];
$stage = $row["Stage"];
$sql3 = 'SELECT * FROM Creatures WHERE Name = "' . $creature . '"';
$result2 = mysqli_query($conn, $sql3);
$row2 = $result2->fetch_assoc();
echo '<input name="ids[]" type="hidden" value="' . $id . '">' .
'<input name="' . $id . 'creature" type="hidden" value="' . $creature . '">' .
'<tr>' .
'<td rowspan="2" style="border:1px solid gray;text-align:center;"><img src="' .
$picture . '"></td>' .
'<td style="border:1px solid gray;text-align:center;">' . $creature . '</td>' .
'<td style"border:1px solid gray;text-align:center;"><select name="' . $id .
'stage">' .
'<option value ="' . $stage . '" selected>' . $stage . '</option>' .
'<option value = "Stage1">Stage1(Egg)</option>' .
'<option value = "Stage2">Stage2</option>';
if ($row2["Stage3"] != "") {
echo '<option value = "Stage3">Stage3</option>';
}
if ($row2["Stage4"] != "") {
echo '<option value = "Stage4">Stage4</option>';
}
echo '</select></td>' .
'<td style="border:1px solid gray;text-align:center;"><select name="' . $id .
'gender">' .
'<option value ="' . $row["Gender"]. '" selected>' . $row["Gender"] . '</option>' .
'<option value = "Unspecified">Unspecified</option>' .
'<option value = "Female">Female</option>' .
'<option value = "Male">Male</option>' .
'</select></td>' .
'<td style="border:1px solid gray;text-align:center;"><select name="' . $id .
'frozen">' .
'<option value ="' . $row["Frozen"] . '" selected>' . $row["Frozen"] . '</option>' .
'<option value="Unspecified">Unspecified</option>' .
'<option value="Yes">Yes</option>' .
'<option value="No">No</option>' .
'</select></td>' .
'</tr>' .
'<tr>' .
'<td style="border:1px solid gray;text-align:center;" colspan="3">Notes: ' .
'<input type="text" name="' . $id .
'notes" value="' . $row["Notes"] .'"></td>' .
'<td style="border:1px solid gray;text-align:center;">' . 'Delete<br>' .
'<input type="checkbox" name="delete[]"
value="' . $id . '"></td>' .
'</tr>';
}
echo '</table><br><input name="submit" type="submit" id="submit" value="Update">' .
'</form><br><br>';
} else {
echo "<br>You have no creatures in your wishlist.";
}
}
}
} catch (mysqli_sql_exception $e) {
throw $e;
}
// Close the connection to the database
$conn->close();
[/insert_php]
</code>
</center>
If you have any ideas what could be causing this please help!

Is your page saved and published?
If not, that might be the issue why it's saying that page is not found.
Also, there is a better way to handle page number generation (or pagination) than the long if else statements you have written.
Please go over this: http://code.runnable.com/U8dzQWEzMxxqeQ_E/php-pagination-example-using-mysql-database-for-dbms
It would also be wise to have a custom Wordpress plugin where you would have all this logic instead of pages since anyone with Wordpress admin access would be able to change the code and you certainly wouldn't want that.

Related

Im having problems with nested forms

Hello im having some problems with my forms. It says "Saw a form start tag, but there was already an active form element. Nested forms are not allowed." but when im looking in my code the forms are not even nested in my code can i have some help.
$sql8 = 'SELECT läggtill.serier, läggtill.id, läggtill.id2, läggtill.säsonger, läggtill.betyg, kategorier.kategori from läggtill inner join'
. ' kategorier on läggtill.kategorier=kategorier.id order by läggtill.serier';
$resultat8 = $mysqli->query($sql8);
$antal8 = $resultat8->num_rows;
while ($rad8 = $resultat8->fetch_assoc()) {
echo "<li>" .$rad8['serier'] .', ' .$rad8['säsonger'] .' Säsonger, ' .$rad8['kategori'] .'<br>'.
'Betyg: ' .$rad8['betyg'] .'/10 </li>' .'<br>';
$id = $rad8['id'];
?>
<?php
echo '<form action="alla.php" method="POST">
<button name=' .$id .'>Ta bort</button>
</form>'
?>
<?php
if (isset($_POST[$id])) {
$sql9 = "Delete from läggtill where id=" . $id . " LIMIT 1";
if ($resultat9 === $mysqli->query($sql9)) {
header('location: alla.php');
} else {
echo "det misslyckades.";
}
echo "asdasd";
}
$id2 = $rad8['id2'];
echo "<div class=" . 'Update' . '>'
?>
<?php
echo "<form action='alla.php' method='POST'>
<label for='serier'>Serie: </label>
<input type='text' name='serier' id='serier'><br>";
$sql14 = 'SELECT * FROM kategorier';
$resultat14 = $mysqli->query($sql14);
$antal14 = $resultat14->num_rows;
if ($antal14 == 0) {
echo 'Inget funnet';
} else {
$sql15 = 'SELECT kategori, id FROM kategorier';
$resultat15 = $mysqli->query($sql15);
$antal15 = $resultat15->num_rows;
if ($antal15 == 0) {
echo 'Inget kategori funnen';
} else {
echo 'Kategori: <select name =kategorier>';
while ($rad15 = $resultat15->fetch_assoc()) {
echo '<option value='. $rad15['id'] . '>' . $rad15['kategori'] . '</option><br>';
}
echo '</select>';
}
}
echo '<br><button name= . $id2' . '>Uppdatera</button>';
if (isset($_POST[$id2])) {
$kategorier = $_POST['kategorier'];
$betyg = $_POST['betyg'];
$serier = $_POST['serier'];
$säsonger = $_POST['säsonger'];
if(strlen($kategorier) && strlen($säsonger) && strlen($serier) && strlen($betyg)) {
$sql12 = '"UPDATE läggtill SET kategorier="" . $kategorier . "", säsonger="" . $säsonger . "", serier="" . $serier . "","
. " betyg="" . $betyg . "" WHERE id2="" . $id2 . "";';
if($resultat9 == $mysqli->query($sql12)) {
header('location: alla.php');
} else {
echo 'Det misslyckades';
}
}
}
}
echo "</form>";
echo "</div>";`**enter code here**`
check have updated few things, hope it will work out
<?php
$sql8 = 'SELECT läggtill.serier, läggtill.id, läggtill.id2, läggtill.säsonger, läggtill.betyg, kategorier.kategori from läggtill inner join'
. ' kategorier on läggtill.kategorier=kategorier.id order by läggtill.serier';
$resultat8 = $mysqli->query($sql8);
$antal8 = $resultat8->num_rows;
while ($rad8 = $resultat8->fetch_assoc()) {
echo "<li>" . $rad8['serier'] . ', ' . $rad8['säsonger'] . ' Säsonger, ' . $rad8['kategori'] . '<br>' .
'Betyg: ' . $rad8['betyg'] . '/10 </li>' . '<br>';
$id = $rad8['id'];
?>
<?php
echo '<form action="alla.php" method="POST">
<button name=' . $id . '>Ta bort</button>
</form>'
?>
<?php
if (isset($_POST[$id])) {
$sql9 = "Delete from läggtill where id=" . $id . " LIMIT 1";
if ($resultat9 === $mysqli->query($sql9)) {
header('location: alla.php');
} else {
echo "det misslyckades.";
}
echo "asdasd";
}
$id2 = $rad8['id2'];
echo "<div class=" . 'Update' . '>'
?>
<?php
echo "<form action='alla.php' method='POST'>
<label for='serier'>Serie: </label>
<input type='text' name='serier' id='serier'><br>";
$sql14 = 'SELECT * FROM kategorier';
$resultat14 = $mysqli->query($sql14);
$antal14 = $resultat14->num_rows;
if ($antal14 == 0) {
echo 'Inget funnet';
} else {
$sql15 = 'SELECT kategori, id FROM kategorier';
$resultat15 = $mysqli->query($sql15);
$antal15 = $resultat15->num_rows;
if ($antal15 == 0) {
echo 'Inget kategori funnen';
} else {
echo 'Kategori: <select name =kategorier>';
while ($rad15 = $resultat15->fetch_assoc()) {
echo '<option value=' . $rad15['id'] . '>' . $rad15['kategori'] . '</option><br>';
}
echo '</select>';
}
}
echo '<br><button name= . $id2' . '>Uppdatera</button></form>';
if (isset($_POST[$id2])) {
$kategorier = $_POST['kategorier'];
$betyg = $_POST['betyg'];
$serier = $_POST['serier'];
$säsonger = $_POST['säsonger'];
if (strlen($kategorier) && strlen($säsonger) && strlen($serier) && strlen($betyg)) {
$sql12 = '"UPDATE läggtill SET kategorier="" . $kategorier . "", säsonger="" . $säsonger . "", serier="" . $serier . "","
. " betyg="" . $betyg . "" WHERE id2="" . $id2 . "";';
if ($resultat9 == $mysqli->query($sql12)) {
header('location: alla.php');
} else {
echo 'Det misslyckades';
}
}
}
}
echo "</div>";

Error on DialFuction !TypeError Object expected

I have an HTML form where I can add first name, last name and phone number. When I try to insert a value into the phone number field I get this error:
message from webpage
For the first name and last name fields it works everytime.I'm using google chrome. Could be a browser error or it is a code error?
This is the part of code which includes the form:
require('libraries/Interview.class.php');
require('libraries/PhoneBook.class.php');
require('libraries/Beneficiari.class.php');
require('libraries/Marci.class.php');
require('libraries/Modele.class.php');
require_once('connect.php');
require('libraries/Agentii.class.php');
require ("include/callcenter-header.php");
$_SESSION['username'] = 'admin';
$phone_no ="";
if (strlen($phone_no) == 9) {
$contact = PhoneBook::listBYPhoneNo($phone_no, $conn);
$selected_contact = null;
$beneficiari = Beneficiari::listAllNoOwner($db);
$content_agentii = "<option value=''>Alege agentie</option>";
$content_beneficiari = "<option value=''>Alege beneficiar</option>";
$btn_add_new = "";
$selected_bname = "";
$selected_aname = "";
if ($contact) {
if (count($contact) == 0) {
$selected_contact = $contact[0];
} else {
for ($x = 0; $x < count($contact); $x++) {
if ($contact[$x]['default'] == 0) {
$selected_contact = $contact[$x];
}
}
}
$agentii = Agentii::listAll_byBeneficiar($selected_contact['id_beneficiar'], $db);
for ($b = 0; $b < count($beneficiari); $b++) {
if ($selected_contact['id_beneficiar'] == $beneficiari[$b]['id']) {
$selected_bname = $beneficiari[$b]['nume'];
};
$selected = ($selected_contact['id_beneficiar'] == $beneficiari[$b]['id'] ? "selected = 'selected'" : '');
$content_beneficiari .= "<option " . $selected . " value='" . $beneficiari[$b]['id'] . "'>" . $beneficiari[$b]['nume'] . "</option>";
}
for ($a = 0; $a < count($agentii); $a++) {
if ($selected_contact['agency_id'] == $agentii[$a]['id']) {
$selected_aname = $agentii[$a]['nume'];
};
$selected = ($selected_contact['agency_id'] == $agentii[$a]['id'] ? "selected = 'selected'" : '');
$content_agentii .= "<option " . $selected . " value='" . $agentii[$a]['id'] . "'>" . $agentii[$a]['nume'] . "</option>";
}
$btn_upd = "<button class='upd_pb'>Actualizeaza info</button>";
} else {
for ($b = 0; $b < count($beneficiari); $b++) {
$content_beneficiari .= "<option value='" . $beneficiari[$b]['id'] . "'>" . $beneficiari[$b]['nume'] . "</option>";
}
$selected_contact = null;
$agentii = null;
$btn_add_new = "<button class='add_new_pb'>Inregistreaza contact</button>";
}
}
?>
<script>
function OnNewCall(myCallTraceId, callParams)
{
$.ajax({
type: "POST",
url: "dynamic/dynamic.functions.cc_interview.php",
data: {ck_new_call: myCallTraceId, callParams: callParams},
success: function (response) {
$('#contact_info_form').html(response);
$('#flt9_assets').val($("#res_client_company option:selected").html());
$('#flt10_assets').val($("#res_client_agency option:selected").html().split(",")[0]);
filterData(1);
}
});
}
</script>
<br/>
<div style="text-align: left" class="contact_info">
<fieldset>
<legend>Informatii client</legend>
<table id="contact_info_form">
<tr>
<td><label>Call Trace ID :</label><br/><input type="text" id="res_ctid" value="-" /></td>
</tr>
<tr>
<td><label>Nume :</label><br/><input type="text" id="res_client_firstname" value="<?php echo #$selected_contact['first_name']; ?>" /></td>
<td><label>Prenume :</label><br/><input type="text" id="res_client_lastname" value="<?php echo #$selected_contact['last_name']; ?>"/></td>
<td><label>Telefon :</label><br/><input type="text" readonly="readonly" id="res_client_phone" value="<?php echo $phone_no; ?>"/></td>
<td><label>Email :</label><br/><input type="text" id="res_client_email" value="<?php echo #$selected_contact['email']; ?>"/></td>
</tr>
<tr>
<td><label>Beneficiar :</label><br/><select id="res_client_company"><?php echo $content_beneficiari; ?></select></td>
<td><label>Agentia :</label><br/><select id="res_client_agency" ><?php echo $content_agentii; ?></select></td>
<td><label>Adresa :</label><br/><input type="text" id="res_client_address" value="<?php echo #$selected_contact['adresa_agentie']; ?>"/></td>
<td><span id="contact_info_act_btn"><?php echo $btn_add_new; ?><?php echo $btn_upd; ?></span></br><span id="btn_act_resp"></span></td>
</tr>
</table>
</fieldset>
<script>
$('#contact_info_form').on('change', '#res_client_company', function () {
$('#flt9_assets').val($("#res_client_company option:selected").html());
$('#flt10_assets').val('');
filterData(1);
$.ajax({
type: "POST",
url: "dynamic/dynamic.functions.agentii.php",
data: {req_benef: $(this).val()},
beforeSend: function () {
$('#res_client_agency').html('');
},
success: function (response) {
$('#res_client_agency').html(JSON.parse(response));
}
});
});
$('#contact_info_form').on('change', '#res_client_agency', function () {
if ($("#res_client_agency option:selected") !== null) {
$('#flt10_assets').val($("#res_client_agency option:selected").html().split(",")[0]);
filterData(1);
}
});
$('#contact_info_form').on('click', '.add_new_pb', function () {
var request = {
first_name: $('#res_client_firstname').val(),
last_name: $('#res_client_lastname').val(),
phone: $('#res_client_phone').val(),
email: $('#res_client_email').val(),
company: $('#res_client_company').val(),
agency: $('#res_client_agency').val(),
address: $('#res_client_address').val()
};
$.ajax({
type: "POST",
url: "dynamic/dynamic.functions.cc_interview.php",
data: {req_new_pb_entry: JSON.stringify(request)},
beforeSend: function () {
$('#contact_info_act_btn').html('');
},
success: function (response) {
var resp = JSON.parse(response);
if (resp['result']) {
$('#contact_info_act_btn').html(resp['html']);
$('#btn_act_resp').html("Contactul a fost inregistrat cu succes !");
} else {
$('#contact_info_act_btn').html(resp['html']);
$('#btn_act_resp').html("Contactul nu a putut fi inregistrat !");
}
}
});
});
$('#contact_info_form').on('click', '.upd_pb', function () {
var request = {
first_name: $('#res_client_firstname').val(),
last_name: $('#res_client_lastname').val(),
phone: $('#res_client_phone').val(),
email: $('#res_client_email').val(),
company: $('#res_client_company').val(),
agency: $('#res_client_agency').val(),
address: $('#res_client_address').val()
};
$.ajax({
type: "POST",
url: "dynamic/dynamic.functions.cc_interview.php",
data: {req_upd_pb_entry: JSON.stringify(request)},
beforeSend: function () {
$('#contact_info_act_btn').html('');
},
success: function (response) {
var resp = JSON.parse(response);
if (resp['result']) {
$('#contact_info_act_btn').html(resp['html']);
$('#btn_act_resp').html("Contactul a fost actualizat cu succes !");
} else {
$('#contact_info_act_btn').html(resp['html']);
$('#btn_act_resp').html("Contactul nu a putut fi actualizat !");
}
}
});
});
$('#res_client_phone').on('click', '', function () {
try
{
window.external.Dial($('#res_client_phone').val(), 0, -1);
} catch (err)
{
alert("Error on Dial Function !" + err);
}
});
</script>
</div>
And this is the dynamic.functions.cc_interview.php code:
require '../connect.php';
/*
* WHEN user selects an equipment
*/
if (isset($_POST['cci_set_et'])) {
$echipID = str_replace('act_lnk_', '', $_POST['cci_set_et']);
require '../libraries/Interview.class.php';
require '../libraries/Echipamente.class.php';
require '../libraries/Ingineri_service.class.php';
$echipament = Echip::listEchipId($echipID, $db);
$ingineri = Ingineri_service::listAllDistinct($db);
$question = Interview::listFirstQuestionByEchip($echipID, $conn);
#$answers = Interview::listAnswers($question[0]['id'], $conn);
$response = array();
$html = "";
$html .= "<tr class='qRow' id='qRow-" . $_POST['q_count'] . "'><td>" . #$question[0]['text'] . "</td>";
$html .= "<td><input type='hidden' id='qR-" . #$question[0]['id'] . "' value=''><input type='hidden' id='qRid-" . $question[0]['id'] . "' value=''>";
for ($x = 0; $x < count($answers); $x++) {
switch (#$question[0]['multiple_choice']) {
case 0 :
$name = "q" . $question[0]['id'] . "";
break;
case 1 : $name = "q" . $question[0]['id'] . "-" . $answers[$x]['id'];
break;
default : $name = "q" . $question[0]['id'] . "";
}
$html .= "<div>" . $answers[$x]['text'] . " <input class='in_qResp' name='" . $name . "' type='" . $answers[$x]['input_type'] . "' id='R-" . $answers[$x]['id'] . "-" . $answers[$x]['next_question_id'] . "' value='" . $answers[$x]['text'] . "' /> ";
if ($answers[$x]['has_hint'] == 1) {
$html .= " | <a class='ahint'>HINT</a><div class='tooltip'>content</div>";
}
$html .= "</div>";
}
$html .= "<button class='qSubmit' id='qS-" . #$question[0]['id'] . "'>Submit</buton></td></tr>";
$ingineri_html = "";
for ($i = 0; $i < count($ingineri); $i++) {
$selected = ($ingineri[$i]['inginer_service'] == $echipament[0]['inginer_service'] ? "selected='selected'" : "");
$ingineri_html .= "<option " . $selected . " value='" . $ingineri[$i]['inginer_service'] . "'>" . ucwords(str_replace('.', ' ', $ingineri[$i]['inginer_service'])) . "</option>";
}
$response['question']['id'] = $question[0]['id'];
$response['equip_class'] = $echipament[0]['categorie'];
$response['equip_mark'] = $echipament[0]['numeMarca'];
$response['equip_model'] = $echipament[0]['numeModel'];
$response['equip_serial'] = $echipament[0]['serie'];
$response['equip_cod'] = $echipament[0]['cod'];
$response['agency_id'] = $echipament[0]['agentie'];
$response['question']['text'] = $question[0]['text'];
$response['question']['response'] = "";
$response['engineer_html'] = $ingineri_html;
$response['html'] = $html;
echo json_encode($response);
}
/*
* When user submits an answer
*/
if (isset($_POST['cci_set_nq'])) {
$answer_token = explode('-', $_POST['cci_set_nq']);
$answer_id = $answer_token[1];
require '../libraries/Interview.class.php';
$selected_answer = Interview::findAnswer($answer_id, $conn);
$question = Interview::listNextQuestionByAnswer($answer_id, $conn);
$answers = Interview::listAnswers($question[0]['id'], $conn);
$response = array();
$html = "";
$html .= "<tr class='qRow'><td>" . $question[0]['text'] . "</td>";
$html .= "<td><input type='hidden' id='qR-" . $question[0]['id'] . "' value=''><input type='hidden' id='qRid-" . $question[0]['id'] . "' value=''>";
for ($x = 0; $x < count($answers); $x++) {
switch ($question[0]['multiple_choice']) {
case 0 :
$name = "q" . $question[0]['id'] . "";
break;
case 1 : $name = "q" . $question[0]['id'] . "-" . $answers[$x]['id'];
break;
default : $name = "q" . $question[0]['id'] . "";
}
$html .= "<div>" . $answers[$x]['text'] . " <input class='in_qResp' name='" . $name . "' type='" . $answers[$x]['input_type'] . "' id='R-" . $answers[$x]['id'] . "-" . $answers[$x]['next_question_id'] . "' value='" . $answers[$x]['text'] . "' /> ";
if ($answers[$x]['has_hint'] == 1) {
$html .= " | <a class='a_hint'>HINT</a><div class='tooltip'></div>";
}
$html .= "</div>";
}
$html .= "<button class='qSubmit' id='qS-" . $question[0]['id'] . "'>Submit</buton></td></tr>";
$response['question']['id'] = $question[0]['id'];
$response['question']['text'] = $question[0]['text'];
$response['question']['response'] = "";
$response['action'] = $selected_answer['action'];
$response['html'] = $html;
echo json_encode($response);
}
/*
* Function to insert new action into table actuni when agent decides so .
*/
if (isset($_POST['cci_set_aNi'])) {
require '../libraries/Actiuni.class.php';
$request = json_decode($_POST['cci_set_aNi']);
$request->equip_id = str_replace('act_lnk_', '', $request->equip_id);
$request->resolution->interview_string = "Reprezentant client : " . $request->client_first_name . " " . $request->client_last_name . " \n " .
"Telefon client : " . $request->call_id . " \n" .
"Rezultat interviu : \n" . $request->resolution->interview_string . " \n";
// $id_sesizare = Actiuni::insert('call_incident', date('Y-m-d H:i'), $request->deadline, $request->engineer, $request->equip_id, $request->resolution->interview_string, 'nevalidata', date('H:i'), $_SESSION['username'], $request->agency_id, '0', 'RON', $db);
$id_sesizare = 212312;
echo $id_sesizare;
}
/*
* Function to insert new Phone Book entry into table phone_book
*/
if (isset($_POST['req_new_pb_entry'])) {
require '../libraries/PhoneBook.class.php';
$request = json_decode($_POST['req_new_pb_entry']);
$inserted = PhoneBook::insert($request->first_name, $request->last_name, $request->phone, $request->email, $request->agency, $conn);
if ($inserted) {
$response['result'] = TRUE;
$response['html'] = "<button class='upd_pb'>Actualizeaza info</button>";
} else {
$response['result'] = FALSE;
$response['html'] = "<button class='add_new_pb'>Inregistreaza contact</button>";
}
echo json_encode($response);
}
if (isset($_POST['req_upd_pb_entry'])) {
require '../libraries/PhoneBook.class.php';
$request = json_decode($_POST['req_upd_pb_entry']);
$updated = PhoneBook::update($request->first_name, $request->last_name, $request->phone, $request->email, $request->agency, $conn);
$response = array();
if ($updated) {
$response['result'] = TRUE;
$response['html'] = "<button class='upd_pb'>Actualizeaza info</button>";
} else {
$response['result'] = FALSE;
$response['html'] = "<button class='upd_pb'>Actualizeaza info</button>";
}
echo json_encode($response);
}
if (isset($_POST['ck_new_call'])) {
$callTraceId = $_POST['ck_new_call'];
$callParams = $_POST['callParams'];
parse_str($callParams, $callParams_arr);
require('../libraries/PhoneBook.class.php');
require('../libraries/Beneficiari.class.php');
require('../libraries/Agentii.class.php');
$phone_no = intval($callParams_arr['MCC_ANI']);
if (strlen($phone_no) == 9) {
$contact = PhoneBook::listBYPhoneNo($phone_no, $conn);
$selected_contact = null;
$beneficiari = Beneficiari::listAllNoOwner($db);
$content_agentii = "<option value=''>Alege agentie</option>";
$content_beneficiari = "<option value=''>Alege beneficiar</option>";
$btn_add_new = "";
$selected_bname = "";
$selected_aname = "";
if ($contact) {
if (count($contact) == 0) {
$selected_contact = $contact[0];
} else {
for ($x = 0; $x < count($contact); $x++) {
if ($contact[$x]['default'] == 0) {
$selected_contact = $contact[$x];
}
}
}
$agentii = Agentii::listAll_byBeneficiar($selected_contact['id_beneficiar'], $db);
for ($b = 0; $b < count($beneficiari); $b++) {
if ($selected_contact['id_beneficiar'] == $beneficiari[$b]['id']) {
$selected_bname = $beneficiari[$b]['nume'];
};
$selected = ($selected_contact['id_beneficiar'] == $beneficiari[$b]['id'] ? "selected = 'selected'" : '');
$content_beneficiari .= "<option " . $selected . " value='" . $beneficiari[$b]['id'] . "'>" . $beneficiari[$b]['nume'] . "</option>";
}
for ($a = 0; $a < count($agentii); $a++) {
if ($selected_contact['agency_id'] == $agentii[$a]['id']) {
$selected_aname = $agentii[$a]['nume'];
};
$selected = ($selected_contact['agency_id'] == $agentii[$a]['id'] ? "selected = 'selected'" : '');
$content_agentii .= "<option " . $selected . " value='" . $agentii[$a]['id'] . "'>" . $agentii[$a]['nume'] . "</option>";
}
$btn_upd = "<button class='upd_pb'>Actualizeaza info</button>";
} else {
for ($b = 0; $b < count($beneficiari); $b++) {
$content_beneficiari .= "<option value='" . $beneficiari[$b]['id'] . "'>" . $beneficiari[$b]['nume'] . "</option>";
}
$selected_contact = null;
$agentii = null;
$btn_add_new = "<button class='add_new_pb'>Inregistreaza contact</button>";
}
$response = "";
$response .="<tr>";
$response .="<td colspan=\"2\"><label>CALL TRACEID :</label><br/><input type=\"text\" id=\"res_ctid\" value=\"" .$callTraceId. "\" /></td>";
$response .="</tr>";
$response .="<tr>";
$response .="<td><label>Nume :</label><br/><input type=\"text\" id=\"res_client_firstname\" value=\"" . #$selected_contact['first_name'] . "\" /></td>";
$response .="<td><label>Prenume :</label><br/><input type=\"text\" id=\"res_client_lastname\" value=\"" . #$selected_contact['last_name'] . "\"/></td> ";
$response .="<td><label>Telefon :</label><br/><input type=\"text\" id=\"res_client_phone\" value=\"" . $phone_no . "\"/></td>";
$response .="<td><label>Email :</label><br/><input type=\"text\" id=\"res_client_email\" value=\"" . #$selected_contact['email'] . "\"/></td>";
$response .="</tr>";
$response .="<tr>";
$response .="<td><label>Beneficiar :</label><br/><select id=\"res_client_company\">" . $content_beneficiari . "</select></td>";
$response .="<td><label>Agentia :</label><br/><select id=\"res_client_agency\" >" . $content_agentii . "</select></td>";
$response .="<td><label>Adresa :</label><br/><input type=\"text\" id=\"res_client_address\" value=\"" . #$selected_contact['adresa_agentie'] . "\"/></td>";
$response .="<td><span id=\"contact_info_act_btn\">" . $btn_add_new . "" . $btn_upd . "</span></br><span id=\"btn_act_resp\"></span></td>";
$response .="</tr>";
error_log($response);
echo $response;
}
}
?>

HTML Form Element with Check boxes

I have some PHP displaying an HTML form like this:
And then updates the information in the table when the update button is pressed.
My issue is with the delete option. Any time I hit the update button, the information IS updated successfully, but I get this error message about the delete statement:
Here is the code:
// Info to connect to the Wishlist database
$servername = ".com";
$dbusername = "";
$password = "";
$dbname = "";
try {
// To connect to the database please
$conn = new mysqli($servername, $dbusername, $password, $dbname);
if ($conn->connect_error) {
die('Connect Error (' . $conn->connect_errno . ') '
. $conn->connect_error);
}
echo "Please click <strong><a href = 'http://eggcavity.com/add-wishlist'>here</a></strong> to add creatures to your wishlist.";
if(isset($_POST['submit'])){
$ids = $_POST['ids'];
// Prepare and bind the udpate statement
$sql2 = "UPDATE Wishlists SET Picture = ?, Stage = ?, Gender = ?, Frozen = ?, Notes= ? WHERE ID = ?";
$stmt2 = $conn->prepare($sql2);
$stmt2->bind_param('sssssi', $picture, $stage, $gender, $frozen, $notes, $id);
foreach($ids as $id){
$stagecode = $id . "stage";
$gendercode = $id . "gender";
$frozencode = $id . "frozen";
$notescode = $id . "notes";
$namecode = $id . "creature";
$stage = $_POST[$stagecode];
$Stage = $stage;
$gender = $_POST[$gendercode];
$frozen = $_POST[$frozencode];
$notes = $_POST[$notescode];
$name = $_POST[$namecode];
$sql1 = 'SELECT * FROM Creatures WHERE Name = "' . $name . '"';
$result = mysqli_query($conn, $sql1);
$row = $result->fetch_assoc();
$picture = $row["$stage"];
$stmt2->execute();
}
$theCount = 0;
foreach($_POST['delete'] as $selected){
$sql = "DELETE FROM Wishlists WHERE ID = ?";
$stmt = $conn->prepare($sql);
$stmt->bind_param('i', $selected);
$stmt->execute();
$theCount++;
}
echo "Your wishlist has been updated, and" .$theCount. " creature(s) has/have been removed from your wishlist.<br>Please click <a href='http://eggcavity.com/edit-wishlist'>here</a> to return to the edit page.";
} else {
// Get current user's username
$current_user = wp_get_current_user();
$username = $current_user->user_login;
$theDeleteCount = 0;
// Just display the form
$sql = 'SELECT Creature, Picture, Stage, Gender, Frozen, ID FROM Wishlists WHERE Username = "' . $username . '"';
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
echo '<form method="POST"><table><strong>' .
'<tr>' .
'<td></td>' .
'<td>Creature</td>' .
'<td>Stage</td>' .
'<td>Gender</td>' .
'<td>Frozen</td>' .
'</tr></strong>';
while($row = $result->fetch_assoc()) {
$creature = $row["Creature"];
$id = $row["ID"];
$picture = $row["Picture"];
$stage = $row["Stage"];
echo '<input name="ids[]" type="hidden" value="' . $id . '">' .
'<input name="' . $id . 'creature" type="hidden" value="' . $creature . '">' .
'<tr>' .
'<td rowspan="2"><img src="' . $picture . '"></td>' .
'<td>' . $creature . '</td>' .
'<td><select name="' . $id . 'stage">' .
'<option value ="' . $stage . '" selected>' . $stage . '</option>' .
'<option value = "Stage1">Stage1(Egg)</option>' .
'<option value = "Stage2">Stage2</option>' .
'<option value = "Stage3">Stage3</option>' .
'<option value = "Stage4">Stage4</option>' .
'</select></td>' .
'<td><select name="' . $id . 'gender">' .
'<option value ="' . $row["Gender"] . '" selected>' . $row["Gender"] . '</option>' .
'<option value = "Unspecified">Unspecified</option>' .
'<option value = "Female">Female</option>' .
'<option value = "Male">Male</option>' .
'</select></td>' .
'<td><select name="' . $id . 'frozen">' .
'<option value ="' . $row["Frozen"] . '" selected>' . $row["Frozen"] . '</option>' .
'<option value="Unspecified">Unspecified</option>' .
'<option value="Yes">Yes</option>' .
'<option value="No">No</option>' .
'</select></td>' .
'</tr>' .
'<tr>' .
'<td colspan="3">Notes: <input type="text" name="' . $id . 'notes" value="' . $row["Notes"] .'"></td>' .
'<td>' . 'Delete<br>' . '<input type="checkbox" name="creatures[]" value="' . $id . '"></td>' .
'</tr>';
}
echo '</table><input name="submit" type="submit" id="submit" value="Update"></form>';
} else {
echo "<br>You have no creatures in your wishlist.";
}
}
} catch (mysqli_sql_exception $e) {
throw $e;
}
// Close the connection to the database
$conn->close();
If you could please help me find what is wrong with the information I am passing to the foreach() statement:
foreach($_POST['delete'] as $selected){
I would be forever grateful. Any idea helps.
I have tried a few many things, a lot of which were found on stackoverflow. I think I am probably missing something small and/or stupid. I have another page running off of a checkbox form which works just fine.
Thank you and have a great day!
The form element that contains the ids of cratures to be deleted is called creatures[] so you need to process the contents of that POST variable rather than delete - even though delete is what you wish to do. SO, perhaps something like this:-
Replace
$theCount = 0;
foreach($_POST['delete'] as $selected){
$sql = "DELETE FROM Wishlists WHERE ID = ?";
$stmt = $conn->prepare($sql);
$stmt->bind_param('i', $selected);
$stmt->execute();
$theCount++;
}
with
$theCount = 0;
$creatures=!empty( $_POST['creatures'] ) ? $_POST['creatures'] : false;
if( $creatures ) {
if( !is_array( $creatures ) ) $creatures=explode(',',$creatures);
foreach( $creatures as $id ){
$sql = "DELETE FROM Wishlists WHERE ID = ?";
$stmt = $conn->prepare($sql);
$stmt->bind_param('i', $id);
$stmt->execute();
$theCount++;
}
}
if deleting is optional every time then just put an variable check like
if(isset($_POST['creatures']))
{
foreach($_POST['creatures'] as $selected){
$sql = "DELETE FROM Wishlists WHERE ID = ?";
$stmt = $conn->prepare($sql);
$stmt->bind_param('i', $selected);
$stmt->execute();
$theCount++;
}
}
this code will run only when it finds $_POST['creatures'] means ur checkbox is checked

mysqli_query() returns something that is NOT an Object

i seem to have a problem with this code, im trying to use mysqli_connect() and mysqli_query() to query the Database, but the code is NOT returning a mysqli Object and i dont know why.
I Made my own code to format the Queries and im sure is not failing because im checking everytime:
( IM USING XAMPP, testing using localhost ), Could it be that the php installed is corrupt?? )
<?php
//error_reporting(~E_ALL);
require 'errorReporting.php';
$mysqli_host = 'localhost';
$mysqli_user = 'root';
$mysqli_pass = NULL;
$mysqli_name = 'gsistel';
$mysqli_link = mysqli_connect($mysqli_host, $mysqli_user, $mysqli_pass, $mysqli_name) or die('SQL ERROR COULD NOT CONNECT TO DB');
mysqli_select_db($mysqli_link, $mysqli_name) or die('SQL ERROR COULD NOT SELECT DB');
function fQuery($table, $operation, $selectionColumns, $iouValues, $whereColumns, $whereValues, $orderBy, $orderClause) {
$operation = strtoupper($operation);
switch($operation) {
case 'SELECT':
global $mysqli_link;
$mysqli_query = 'SELECT ';
$selection = '';
for($i = 0; $i < count($selectionColumns); $i++) {
if(count($selectionColumns) == 1) {
$selection = $selectionColumns[0];
}else if($i == count($selectionColumns)-1) {
$selection = $selection . $selectionColumns[$i];
}else {
$selection = $selection . $selectionColumns[$i] . ', ';
}
}
$mysqli_query = $mysqli_query . $selection . ' FROM ' . $table . ' ';
$whereSelection = '';
if($whereColumns != null && (count($whereColumns == count($whereValues)))) {
$mysqli_query = $mysqli_query . ' WHERE ';
for($i = 0; $i < count($whereColumns); $i++) {
if(count($whereColumns) == 1) {
$whereSelection = $whereColumns[0] . '=\'' . $whereValues[0] . '\'';
}else if($i == 0){
$whereSelection = $whereSelection . $whereColumns[$i] . '=\'' . $whereValues[$i] . '\'';
}else {
$whereSelection = $whereSelection . ' AND ' . $whereColumns[$i] . '=\'' . $whereValues[$i] . '\'';
}
}
$mysqli_query = $mysqli_query . $whereSelection . ' ';
}
if($orderBy != null) {
$mysqli_query = $mysqli_query . ' ORDER BY ' . $orderClause;
}
return $mysqli_result = mysqli_query($mysqli_link, $mysqli_query) or die(reportFriendlyError('SQL SELECT', 'ERROR: SQL QUERY FAILED<br>' . $mysqli_query));
break;
case 'INSERT':
$mysqli_query = 'INSERT INTO ' . $table . ' VALUES (';
for($i = 0; $i < count($iouValues); $i++) {
if($i == count($iouValues)-1) {
if(strtoupper($iouValues[$i]) == 'NULL') {
$mysqli_query = $mysqli_query . $iouValues[$i] . ')';
}else {
$mysqli_query = $mysqli_query . '\'' . $iouValues[$i] . '\'' . ')';
}
}else {
if(strtoupper($iouValues[$i]) == 'NULL') {
$mysqli_query = $mysqli_query . $iouValues[$i] . ', ';
}else {
$mysqli_query = $mysqli_query . '\'' . $iouValues[$i] . '\'' . ', ';
}
}
}
return $mysqli_query;
//TODO: Executa query
break;
case 'UPDATE':
$mysqli_query = 'UPDATE ' . $table . ' SET ';
for($i = 0; $i < count($selectionColumns); $i++) {
if($i == count($selectionColumns)-1) {
$mysqli_query = $mysqli_query . $selectionColumns[$i] . '=\'' . $iouValues[$i] . '\'';
}else {
$mysqli_query = $mysqli_query . $selectionColumns[$i] . '=\'' . $iouValues[$i] . '\', ';
}
}
if($whereColumns != null && (count($whereColumns) == count($whereValues))) {
$mysqli_query = $mysqli_query . ' WHERE ';
for($i = 0; $i < count($whereColumns); $i++) {
if($i == count($whereColumns)-1) {
$mysqli_query = $mysqli_query . $whereColumns[$i] . '=\'' . $whereValues[$i] . '\'';
}else {
$mysqli_query = $mysqli_query . $whereColumns[$i] . '=\'' . $whereValues[$i] . '\', ';
}
}
}
return $mysqli_query;
//TODO: Execute query
break;
case 'DELETE':
$mysqli_query = 'DELETE FROM ' . $table . ' WHERE ';
for($i = 0; $i < count($whereColumns); $i++) {
if($i == count($whereColumns)-1) {
$mysqli_query = $mysqli_query . $whereColumns[$i] . '=\'' . $whereValues[$i] . '\'';
}else {
$mysqli_query = $mysqli_query . $whereColumns[$i] . '=\'' . $whereValues[$i] . '\', ';
}
}
return $mysqli_query;
//TODO: execute query
break;
default:
return false;
break;
}
}
?>
im using the code here to test:
The mysqli_query() returns something other than false, but its not a mysqli Object.
I also get the warning:
Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in C:\xampp\htdocs\Guardian2\login.php on line 19
<?php
require 'connect.php';
$mysqli_object = fQuery('users', 'SELECT', array('username'), null, array('username') , array('admin'), null, null);
if($mysqli_object === false) {
echo 'FAIL';
}
if(is_object($mysqli_object)) {
echo 'IS OBJECT';
}else {
echo 'IS NOT OBJECT';
}
$user = '';
while($row = mysqli_fetch_array($mysqli_object)) {
echo $user = $row['username'];
}
?>
Replace:
return $mysqli_result = mysqli_query($mysqli_link, $mysqli_query) or die(reportFriendlyError('SQL SELECT', 'ERROR: SQL QUERY FAILED<br>' . $mysqli_query));
with:
$mysqli_result = mysqli_query($mysqli_link, $mysqli_query) or die(reportFriendlyError('SQL SELECT', 'ERROR: SQL QUERY FAILED<br>' . $mysqli_query));
return $mysqli_result;
in your case 'SELECT' branch.

Displaying PHP results?

I need a little help here with some php.
just a little explanation:
(im trying to display 5 results using this code
$n_id = mysql_real_escape_string ($_GET['id']);
$path = '';
if(isset($n_id) && $n_id != "") {
$sql = 'SELECT * FROM test2 WHERE id="' . $n_id . '"';
$news = mysql_query($sql);
if($result = mysql_fetch_array($news)) {
$title = mysql_result($news,0,"title");
$date = mysql_result($news,0,"date");
echo '<b>' . $title . ' | ' . $date . '</b>
<br/>
<img src="images.php?id='. $n_id .'>';
} else {
header("Location: vendi.php");
}
echo '<br />Back to Archive';
}
It does display but i want that 1st result to be (image+title of the news and other results to be just title).
Hope i wrote it clearly what i needed help with.
Thank you
Your SQL statement is only fetching a single row. This isn't a complete solution, but should get you closer:
if (isset($_GET['id']) && is_numeric($_GET['id'])) {
$n_id = (int)$_GET['id'];
$path = '';
$count = 0;
$sql = 'SELECT * FROM test2 WHERE id BETWEEN ' . $n_id ' AND ' . ($n_id + 5);
$news = mysql_query($sql);
while ($result = mysql_fetch_array($news)) {
$title = $result['title'];
$date = $result['date'];
echo '<b>' . $title . ' | ' . $date . '</b>';
if ($count < 1) {
echo '<br/><img src="images.php?id='. $n_id .'>';
$count++;
}
}
if ($count == 0) { header("Location: vendi.php"); }
echo '<br />Back to Archive';
}
This should do it.
$n_id = mysql_real_escape_string ($_GET['id']);
$path = '';
if(isset($n_id) && $n_id != "") {
$sql = 'SELECT * FROM test2 WHERE id="' . $n_id . '"';
$news = mysql_query($sql);
$first = TRUE;
if($result = mysql_fetch_array($news)) {
$title = mysql_result($news,0,"title");
$date = mysql_result($news,0,"date");
if($first == TRUE) {
echo '<b>' . $title . ' | ' . $date . '</b>';
$first = FALSE;
}
else {
echo '<b>' . $title . ' | ' . $date . '</b>
<br/>
<img src="images.php?id='. $n_id .'>';
}
}
else {
header("Location: vendi.php");
}
echo '<br />Back to Archive';
}
}
While looping through your results, you can check if you are at the first result:
$counter = 0;
while ($row = mysql_fetch_array($news)) {
$title = mysql_result($news,0,"title");
$date = mysql_result($news,0,"date");
echo '<b>' . $title . ' | ' . $date . '</b>';
if ( $counter == 0 ) {
echo '<br /><img src="images.php?id='. $n_id .'>';
}
}

Categories