This is the code I'm using for deleting row from my DB:
<?php
$eid = $_GET['eid'];
$con = mysqli_connect("localhost", "root", "","project") or die("Connection failed");
echo "connection is done";
$query = "delete from exam where eid='$eid'";
if ($con->query($query)==TRUE)
{
echo " record deleted";
}
else
{
echo "Error: " . $query . "<br>" . $con->error;
}
$con->close();
?>
The else statement is not getting executed. It displays "record deleted" for every value even if the value is not found in the database.
Why is this happening? how can I verify that my record has been deleted from my DB?
You can use mysqli.affected-rows.
Consider the following:
$query="delete from exam where eid='$eid'";
if ($con->query($query)==TRUE && $con->affected_rows > 0) {
echo " record deleted";
} else {
echo "Error: " . $query . "<br>" . $con->error;
}
Related
So I am trying to display an array from a database that I have. When I run the script the script, I get an internal server error. Now I am not sure if this has to do with my config script or if I am not cycling through my array properly.
include 'config.php';
$conn = name2;
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT Feild FROM Season 1";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "Feild " . $row["Feild"]. " "<br>";
}
} else {
echo "0 results";
}
The syntax you have used to display the results was incorrect, replace:
echo "Feild " . $row["Feild"]. " "<br>";
With:
echo 'Feild '.$row["Feild"].'<br>';
You have not terminated your string properly.
Replace
echo "Feild " . $row["Feild"]. " "<br>";
With
echo "Feild " . $row["Feild"]. "<br>";
If you had errors turned on you would be getting an error stating a line number.
We need to know which column you're selecting from, as "SELECT Feild FROM Season 1" isn't valid. It should be something like "SELECT Feild FROM Season WHERE column = '1'"
With that in mind, this gets you closer to a solution:
include 'config.php';
$conn = name2;
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT Feild FROM Season 1";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "Feild ". $row["Feild"] ."<br>";
}
} else {
echo "0 results";
}
Use correct syntax
echo "Feild ".$row['Feild']."<br>";
How do I handle variables from POST requests? Lets say I have tables like this And I want to update the votes variable wherever a specific id is.
So for the code I have this
$vote = $_POST["votes"];
$sentid = $_POST["sentid"];
along with something like this
UPDATE `my_exampleo202s`.`President Candidates` SET votes = votes + 1 WHERE `President Candidates`.`id` = sentid
And in the post request I send the sentid with a number. sentid=3
This doesn't work though. I'm not able to give any errors or anything because I'm not viewing it from a browser.
Any idea what the proper way is that I'm supposed to do this?
(Here's the code I have right now if it's needed)
<?php
$vote = $_POST["votes"];
$sentid = $_POST["sentid"];
$conn = new mysqli("localhost","exampleo202s","","my_exampleo202s");
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "UPDATE `my_exampleo202s`.`President Candidates` SET votes = votes + 1 WHERE `President Candidates`.`id` = sentid";
if ($conn->query($sql) === TRUE) {
echo "<br>Record updated successfully <br>";
} else {
echo "<br>Error updating record: <br>" . $conn->error;
}
$conn->close();
?>
UPDATE
<?php
$vote = $_POST["votes"];
$sentid = $_POST["sentid"];
$conn = new mysqli("localhost","jusavoting10rxx9s3","","my_jusavoting10rxx9s3");
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "UPDATE `my_jusavoting10rxx9s3`.`President Candidates` SET votes = votes + 1 WHERE `President Candidates`.`id` = $sentid";
if ($conn->query($sql) === TRUE) {
echo "<br>Record updated successfully <br>";
} else {
echo "<br>Error updating record: <br>" . $conn->error;
}
$conn->close();
?>
Try echo your $_POST value if it doesn't work:
<?php
$vote = $_POST["votes"];
$sentid = $_POST["sentid"];
$conn = new mysqli("localhost","exampleo202s","","my_exampleo202s");
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "UPDATE `my_exampleo202s`.`President Candidates` SET votes = votes + 1 WHERE `President Candidates`.`id` = $sentid";
if ($conn->query($sql) === TRUE) {
echo "<br>Record updated successfully <br>";
} else {
echo "<br>Error updating record: <br>" . $conn->error;
}
$conn->close();
?>
When I print my code it only prints the question and description of id = 1 but not the rest of the table.
here is my code.
Please show me how to print my entire table which has like 20 questions or so...and also please show me how to make it so that the questions stay on the browser (even when I refresh the page) because currently the data does not stay on the browser when i refresh the page.
Thanks So Much!
<?php
require_once "connection.php";
if(isset($_POST['submit'])) {
$conn = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME );
if($conn->connect_error) {
die("connection error: " . $conn->connect_error);
} else {
echo "Submit button connected to database!";
}
$question = $_POST['question'];
$description = $_POST['description'];
$sql = " INSERT INTO `ask` (question_id, question, description) VALUES
(NULL, '{$question}', '{$description}' ) ";
if($conn->query($sql)) {
echo "it worked";
} else {
echo "error: " . $conn->error;
exit();
}
$query = "SELECT * FROM `ask` ";
if( $result = $conn->query($query)) {
$fetch = $result->fetch_assoc();
echo "<p>{$fetch['question']}</p>";
echo "<p>{$fetch['description']}</p>";
} else {
echo "failed to fetch array";
}
}
?>
You need a for each loop:
<?php
require_once "connection.php";
if(isset($_POST['submit'])) {
$conn = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME );
if($conn->connect_error) {
die("connection error: " . $conn->connect_error);
} else {
echo "Submit button connected to database!";
}
$question = $_POST['question'];
$description = $_POST['description'];
$sql = " INSERT INTO `ask` (question_id, question, description) VALUES
(NULL, '{$question}', '{$description}' ) ";
if($conn->query($sql)) {
echo "it worked";
} else {
echo "error: " . $conn->error;
exit();
}
$query = "SELECT * FROM `ask` ";
if( $result = $conn->query($query)) {
$fetch = mysql_fetch_array($result, MYSQL_ASSOC);
foreach($fetch as $ques) {
echo "<p>" . $ques['question'] . "</p>";
echo "<p>" . $ques['description'] . "</p>";
}
} else {
echo "failed to fetch array";
}
}
?>
All I've done there is change:
$fetch = $result->fetch_assoc();
echo "<p>{$fetch['question']}</p>";
echo "<p>{$fetch['description']}</p>";
to:
$fetch = mysql_fetch_array($result, MYSQL_ASSOC);
foreach($fetch as $ques) {
echo "<p>" . $ques['question'] . "</p>";
echo "<p>" . $ques['description'] . "</p>";
}
fetch_assoc() — Fetch a result row as an associative array
so it gets only 1 row you need to loop through the rest of the rows check the examples reference from php docs
I am trying to search some data from a database. The search works fine, however if I click on search without entering anything into the form, it displays all the data on the database. Anyway I can fix this?
This is my php code.
$link=mysqli_connect("localhost","root","");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$db_selected = mysqli_select_db($link,"AnimalTracker1");
if (!$db_selected)
{
die ("Can\'t use test_db : " . mysqli_error($link));
}
$searchKeyword = $_POST['find']; // Sanitize this value first !!
$sql=mysqli_query($link, "Select * FROM Locations WHERE `Animal_Type` LIKE '%$searchKeyword%' ");
if ($sql == FALSE)
{
die($sql." Error on query: ".mysqli_error($link));
}
while($result = mysqli_fetch_array($sql))
{
echo $result ['Animal_Type'];
echo "<br>";
echo $result ['Latitude'];
echo "<br> ";
echo $result ['Longitude'];
echo " <br>";
echo $result ['Seen'];
echo " <br> ";
echo $result ['Time'];
echo "<br> ";
echo "<br> ";
}
//}
?>
Just make sure $searchKeyword has a (valid) value
$link=mysqli_connect("localhost","root","");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$db_selected = mysqli_select_db($link,"AnimalTracker1");
if (!$db_selected)
{
die ("Can\'t use test_db : " . mysqli_error($link));
}
// checks to see if $_POST['find'] is actually set.
if ( array_key_exists('find',$_POST) )
{
$searchKeyword = $_POST['find']; // Sanitize this value first !!
// sanitize $searchKeyword here
}
// checks to see if $searchKeyword has no value, or just contains empty space
if ( empty(trim($searchKeyword)) )
{
echo "You must enter a search term";
}
else
{
$sql=mysqli_query($link, "Select * FROM Locations WHERE `Animal_Type` LIKE '%$searchKeyword%' ");
if ($sql == FALSE)
{
die($sql." Error on query: ".mysqli_error($link));
}
while($result = mysqli_fetch_array($sql))
{
echo $result ['Animal_Type'];
echo "<br>";
echo $result ['Latitude'];
echo "<br> ";
echo $result ['Longitude'];
echo " <br>";
echo $result ['Seen'];
echo " <br> ";
echo $result ['Time'];
echo "<br> ";
echo "<br> ";
}
}
?>
Try removing the "%" from either the back or the front of the $searchKeyword in the query and I guess it should do the work.
"%" is used when match all or none. So if you send an empty string it will return the whole database.
Trying to get all the users from my database, yet when I do so, the query fails.
Attempting to do so with
$userNames = mysqli_query($con, "SELECT * FROM Login");
Where in PHPMyAdmin the database has a few records in the table login.
I checked if the connection is connected, its connected.
Is there any reason this wouldn't work?
Here is a examplepage, you can try logging in with Username and Password.
EDIT: From the example page, here is the code used to test:
if (mysqli_connect_errno($con)) {
echo "Failed to connect to MySQL: " . mysqli_connect_error() . "<br />";
} else {
echo "Connected to MySQL!<br />";
}
if(mysqli_query($con, "SELECT * FROM Login")){
echo "Query good!<br />";
} else {
echo "Query bad!<br />";
}
EDIT 2: Here is a screenshot of the table existing, and the data existing:
I use this
$con = StartDatabase("localhost", "username", "password", "mydatabase");
function StartDatabase($dblocation, $dbuser, $dbpasswd, $dbname){
$link = mysqli_connect($dblocation, $dbuser, $dbpasswd, $dbname);
if (!$link) {
die('Connect Error (' . mysqli_connect_errno() . ') '
. mysqli_connect_error());
}
return $link;
}
if(mysqli_query($con, "SELECT * FROM Login")){
echo "Query good!<br />";
} else {
echo "Query bad!<br />";
}
Try adding this to the else of the query to get an error output:
echo "Error code ({$con->errno}): {$con->error}";
Try like this:
$con = new mysqli("pdb1.awardspace.com", "*******", "****", "*******");
$query = "SELECT * FROM Login";
if ( !$con->query($query) ) {
echo "Query bad!<br />";
echo mysqli_connect_error() . "<br />";
echo "Error code ({$sql->errno}): {$sql->error}";
} else {
echo "Query good!<br />";
}
$con->close();