PHP - MySQL - Delete Row - mysql

I am not too sure what i am doing wrong. i am trying to delete the entire row with this code but it is not working. No error is happening it prints the line that it was deleted but when i go and have a look it is not working. Any thoughts?
<?
$con = mysql_connect("localhost","username","password");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("theobse1_scores", $con);
$sql="DELETE FROM times WHERE id='$id'";
if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
echo "1 record deleted go back to delete another!";
mysql_close($con)
?>

You used $order instead of your query variable $sql
$sql="DELETE FROM times WHERE id='$id'";
mysql_query($sql);

I got it working using this code!
<?php
$id =$_REQUEST['id'];
$con = mysql_connect("localhost","username","password");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("database", $con);
// sending query
mysql_query("DELETE FROM times WHERE id = '$id'")
or die(mysql_error());
?>

else
{
$qry = "SELECT * FROM my_login WHERE email = '".$email."' LIMIT 1";
$res = mysql_query($qry);
if(mysql_num_rows($res) > 0)
{
echo "Email already exists!";
}
else
{
$qry="INSERT INTO my_login SET name='$name',city='$city',comment='$comt',password='$pass',email='$email'";
mysql_query($qry);
}
}
}
?>

delete.php
<?php
include "connect.php";
$id =$_REQUEST['id'];
// sending query
mysql_query("DELETE FROM utilizatori WHERE id = '$id'")
or die(mysql_error());
?>
is corect, I tested and delete from ID
and here is the button delete: `
<?Delete`?>

Related

exclude status in dropdown list

I'm trying to exclude all values where status is Defective in my mobo table, but this code doesn't work.
It's still showing everything in my Dropdown
<?php
$conn = new mysqli('localhost', 'root', 'admin2018', 'inventory')
or die ('Cannot connect to db');
$result = $conn->query("select mobo, status FROM mobo WHERE NOT 'status = Defective'");
echo "<select name='mobo'>";
while ($row = $result->fetch_assoc()) {
unset($id, $name);
$id = $row['mobo'];
$name = $row['mobo'];
echo '<option value="'.$id.'">'.$name.'</option>';
}
echo "</select>";
?>
nevermind I already fix it using
<?php
$conn = new mysqli('localhost', 'root', 'admin2018', 'inventory')
or die ('Cannot connect to db');
$result = $conn->query("select mobo, status FROM mobo WHERE status!='Defective'");
echo "<select name='mobo'>";
while ($row = $result->fetch_assoc()) {
unset($id, $name);
$id = $row['mobo'];
$name = $row['mobo'];
echo '<option value="'.$id.'">'.$name.'</option>';
}
echo "</select>";
?>

i have issue with my database mySQL select from table

Dears
Please help me in this matter.
I have issue with my MySQL database.
It is working fine if I'm doing inserting records to the table. However, if I'm doing selecting and fetching, the result is 0 despite of the table actually have records.
$sql2 = 'SELECT * FROM `users`';
$result2 = $conn->query($sql2);
echo $result2->num_rows;
if ($result2->num_rows > 0) {
// output data of each row
while($row = $result2->fetch_assoc()) {
echo "id: " . $row["id"]. " - Name: " . $row["firstname"]. " " . $row["lastname"]. "<br>";
}
} else {
echo "0 results";
}
please help me guys I tried every thing I almost give up.
thanks,
Maybe you can try this code
<?php
try {
$conn = new PDO('mysql:host=localhost;dbname=contoh', "root", "root");
$conn->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
$sql2 = 'SELECT * FROM `user_ranks`';
$result2 = $conn->query($sql2);
while($row = $result2->fetch()) {
$id = $row['id'];
$username = $row['username'];
$password = $row['password'];
$rank = $row['rank'];
}
}
catch (PDOException $e) {
print "connection or query have a problem: " . $e->getMessage() . "<br/>";
die();
}

Working script stop working (php,mysql)

I've got a weird problem.
I created a website on localhost. Everything was working fine. I could connect to my mysql db without any problems.
I decided to put it on external server to show it to my friends. I changed $username, $dbname etc.
Then I decided to work little with the website back on localhost. And then it started. When I run my script which adds new records to my db (name, message, email) this shows up.
No database selected
Everything is just the same it was before I decided to put it on the server. What can be the problem?
EDIT: Here's the code of connect.php
<?php
$username ="root";
$password = "";
$host = "localhost";
$table = "test";
$conn = new mysqli("$host", "$username", "$password", "$table");
if (!$conn) {
die("Nie udało się połączyć z bazą danych: ".mysqli_connect_error());
}
and here's a script implemented into index.php
<?php
include 'connect.php';
$sql = "SELECT s_id, name, s_like, s_dislike, message FROM suggestions ORDER BY rand()";
$result = mysqli_query($conn, $sql);
$row = mysqli_fetch_assoc($result);
$s_id = $row["s_id"];
$s_like = $row["s_like"];
$s_dislike = $row["s_dislike"];
echo '<p>' . $row["message"] . '</p>';
echo '<h1 class="podpis">' . $row["name"] . '</h1>';
mysqli_close($conn);
?>
and here's the cde of comments.php where I see the message about No database selected.
<?php
$name = $_REQUEST['name'];
$email = $_REQUEST['email'];
$message = $_REQUEST['message'];
$username ="root";
$password = "";
$host = "localhost";
$table = "test";
$conn = mysqli_connect($host, $username, $password, $table);
$query = "INSERT INTO suggestions (name, email, message) VALUES ('$name',
'$email', '$message')";
if (mysql_query($query)) {
echo 'Dodano sugestię!';
header('Location: index.php');
} else{ die(mysql_error());}
mysql_close();
?>

How to display a query from a phpmyadmin that uses MariaDB? My query returns empty

Can someone please explain to me how to display a query from a phpmyadmin that uses MariaDB? When I execute this script, the page returns empty. No error messages, nothing. All blank screenshot of the phpamydmin here.
It seems the commands I shall use in PHP must be different from the used in the conventional phpmyadmin.
I've searched in different sites, but it seems MariaDB is not much used.
<?php
$dbhost = 'localhost:3306';
$dbuser = 'santos1q_test';
$dbpass = 'xxxxxx';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
die('Could not connect: ' . mysql_error());
}
$sql = 'SELECT * FROM test';
mysql_select_db('santos1q_events');
$retval = mysql_query( $sql, $conn );
if(! $retval )
{
die('Could not get data: ' . mysql_error());
}
while($row = mysql_fetch_array($retval, MYSQL_ASSOC))
{
echo {$row['event_title']};
echo {$row['event_address']};
}
echo "Fetched data successfully";
mysql_close($conn);
?>

i created a table in database to display photos and i made it to display ,but i want them to display from the last to the first

<?php
$conn = mysql_connect("localhost","root","");
if(!$conn){
echo mysql_error();
}
$db = mysql_select_db("imagestore",$conn);
if(!$db ){
echo mysql_error();
}
$q = "SELECT * FROM imagetable";
$r = mysql_query("$q",$conn);
if($r)
{
while($row=mysql_fetch_array($r) )
{
header("Content-type: text/html");
echo "</br>";
echo $row['photoname'];
echo "</br>";
$type = "Content-type: ".$row['phototype'];
header($type);
echo "<img src=image.php?fotoid=". $row['fotoid']."width =300 height = 35. 300/>";
}
}
else{
echo mysql_error();
}
?>
I know ORDER by but is not working to display photos as i want in my page .
I am a beginner .
I used $q = "SELECT * FROM imagetable ORDER BY fotoid DESC";