Delete an entire row from a table, using MySQL - mysql

I am trying to code for a row to be deleted in MySQL.
I am not sure how to do this so have picked some coding up from another website:
Syntax:
<?php
$personID=$_GET["q"];
if ($personID<="0"){echo( "<center><h3>NO ID ERROR CLICK THIS TO RETRY CLOSE WINDOW</h3></center>");
}
else
{
$con = mysql_connect("mysql.XXX.com","XXX","XXX");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
$myid=$_COOKIE["user"];
#mysql_select_db("u716720408_admin", $con)or die( "Unable to select database");
$result=mysql_query("DELETE * FROM users WHERE personID=$personID")or die(mysql_error());
$cnt=1;
$row = mysql_fetch_array( $result );
$f1=$row['personID'];
$f2=$row['personFname'];
$f9=$row['llmail'];
$f14=$row['personSname'];
$f19=$row['password'];
?>

Use:
DELETE FROM users WHERE personID=$personID
Make sure you read up about SQL Injection

You don't need the * in the delete query
try
DELETE FROM users WHERE personID=$personID

In your result you dont need to metion * in delete query
$result = mysql_query("DELETE FROM users WHERE personID=$personID") or
die(mysql_error());

Related

Cannot push data to mysql

I can't seem to push data from my form to the database, I have checked the error_log and there's no error. Please check my codes below, thank you!
<?php
$con=mysqli_connect("localhost","admineventus","J7!ren;3") or Die ("Cannot connect");
mysqli_select_db($con,"eventus");
if(isset($_POST['submitpublish']))
{
mysqli_query($con,"insert into events values('$_POST[Category]','$_POST[Name]','$_POST[Location]','$_POST[Sdate]','$_POST[Edate]','$_POST[Etime]','$_POST[Fee]','$_POST[Free]','$_POST[About]')");
}
?>
Try this
if(isset($_POST['submitpublish']))
{
$sql = "INSERT INTO events (col1, col1, .. , colx)
VALUES ('".$_POST["Category"]."','".$_POST["Name"]."','".$_POST["Location"]."','".$_POST["Sdate"]."','".$_POST["Sdate"]."','".$_POST["Edate"]."','".$_POST["Etime"]."','".$_POST["Fee"]."','".$_POST["Free"]."','".$_POST["About"]."')";
$result = mysqli_query($conn,$sql);
}
Make sure your code enters in IF statement

How to save output table within the DB

I run a computation query and displayed an output table, instead of exporting it as csv file, i want is to save it or make it another table within my DB, is it possible?
Yes you can..
Create a table first to your database.
Then write a query to insert data to your created table.
$getTheDataFromYourPrevQuery = //set your data here
$sql = "INSERT INTO yourTable (name)"." VALUES('$getTheDataFromYourPrevQuery')";
try {
$db = $this->db;
$stmt = $db->prepare($sql);
$stmt->execute();
//$user->id = $db->lastInsertId();
$db = null;
echo json_encode($user);
} catch(PDOException $e) {
//error_log($e->getMessage(), 3, '/var/tmp/php.log');
echo '{"error":{"text":'. $e->getMessage() .'}}';
}

Admin panel - Creating an edit users button

I've created an admin panel on my website so when the admin logs in he can edit users. I'm trying to get it to create a table that displays a list of all the users on the database, however, when I run it I get the error:
No database selected
Here is the code in my editusers.php:
<?php
include 'adminpage.php';
include 'connection.php';
$sql = "SELECT * FROM Users";
$result = mysql_query($sql)or die(mysql_error());
echo "<table>";
echo "<tr><th>UserID</th><th>First Name</th><th>Last Name</th><th>Email</th><th>D-O-B</th></tr>Username</th><th>Password</th><th>";
while($row = mysql_fetch_array($result)){
$userid = $row['UserID'];
$firstname = $row['FirstName'];
$lastname = $row['LastName'];
$email = $row['Email'];
$dob = $row['DateofBirth'];
$username = $row['Username'];
$password = $row['Password'];
// Now for each looped row
echo "<tr><td style='width: 200px;'>".$userid."</td><td style='width: 200px;'>".$firstname."</td><td>".$scale."</td><td>".$lastname."</td><td>".$email."</td></tr>".$dob."</td></tr>".$username."</td></tr>".$password."</td></tr>";
} // End our while loop
echo "</table>"
?>
First of all it looks like you are using mysql which isn't a wise move. This is because Mysql is actually deprecated and was improved to mysqli. Your problem may be to do with your database connection. You also haven't set a database. Like I said you can set an active database in your connection script. It should or could look something like this.
<?php
$conn = mysqli_connect("localhost", "root", "password", "database");
// Evaluate the connection
if (mysqli_connect_errno()) {
echo mysqli_connect_error();
exit();
}
?>
After that, your sql query is correct by selecting all from you table 'users' but in order to proceed I recommend creating a query where you use mysqli_query an select the $sql and $conn as parameters. In all honesty it is much advised to stop and continue once you have adapted to mysqli. Alternatively you can use PDO which in some cases can be seen as better to use rather than mysqli but the choice is yours. I personally would get to grips with mysqli and then look at some answers on Stack Overflow to decide whether you should use PDO or not. Visit the PHP manual here. Enter all the mysql functions you know and it will show you how to use the new mysqli version of the functions. Don't think that it is as simple as just adding and 'i' to the end of a mysql function. That's what I initially thought but there is alot to do with extra parameters etc. Hope this helps :-)

mysql max function does not work in joomla

someone can tell me what is wrong in this code? I just want to get the last date in Joomla 2.5. Thanks
// Get a db connection.
$db = JFactory::getDbo();
// Create a new query object.
$query = $db->getQuery(true);
$query
->select($db->quoteName('MAX(created)'))
->from($db->quoteName('#__content'))
$db->setQuery($query);
$result = $db->loadResult();
return $result;
don't quote functions:
$query
->select('MAX('.$db->quoteName('created').')')
->from($db->quoteName('#__content'));
A ; is missing at the end of the line ->from($db->quoteName('#__content'))

use a single return from a sql query

I'm using PHP to make a very specific sql query. For example sake, I have the user's ID number, but I need their name. So I do a sql query from that table with the ID number in order to return the name.
$result = mysql_query("SELECT name FROM users WHERE userID=$thisuserid",$db);
Now I want to use that. What's the most succinct way to go about making that result into a variable ths I can use?
edit:
I'm hoping that this is not the answer:
$rowCheck = mysql_num_rows($result);
if ($rowCheck > '0') {
while ($row = mysql_fetch_assoc($result)){
foreach ($row as $val){
$username = $val;
}
}
}
I have used something like this to keep it short in the past:
list($name) = mysql_fetch_row(mysql_query("SELECT name FROM users WHERE userID=$thisuserid",$db));
echo $name;
In my opinion, the best way to fetch any SQL result is through mysql_fetch_assoc(). To use it, you would do something like this:
$result = mysql_query("SELECT name FROM users WHERE userID=$thisuserid",$db);
while ($row = mysql_fetch_assoc($result)) {
echo $row['name']; // You get an array with each column returned from your query.
}
Still, MySQL extension has been replaced for MySQLi, which is acknowledged to be faster and more practical. It has both OOP and structural bindings, and takes more into account your server settings.
$result = mysql_query("SELECT name FROM users WHERE userID=$thisuserid",$db);
if (!$result) {
echo 'Could not run query: ' . mysql_error();
exit;
}
$name = mysql_fetch_row($result)[0];
You should use MySQLi as bellow:
$db = new MySQLi($host,$user,$pass,$db);
$query = $db->query('SELECT name FROM users WHERE userID='.$thisuserid);
$result = $query->fetch_object();
echo $result->name;
If you use SELECT * so you also can access via $result->{field_name}