Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I'm using PHP to display what is in my MySQL database in a table. I think it is working but the output is still "ERROR". I need to delete all records in a row.
<?php
require_once ('config.inc.php');
$id=$_POST['id'];
$sql = "DELETE `subject_information` WHERE `id`='$id'";
$result = mysql_query($sql);
if ($result)
{
echo "Deleted Successfully";
}
else
{
echo "ERROR!";
mysql_close();
}
?>
You forgot your FROM keyword. The proper syntax is:
DELETE FROM table_name
WHERE some_column=some_value;
So your code should be like this:
$sql = "DELETE FROM `subject_information` WHERE `id`='$id'";
you should change this line :
$sql = "DELETE `subject_information` WHERE `id`='$id'";
to
$sql = "DELETE FROM `subject_information` WHERE `id`='".$id."'";
First you should output the error that is returned from the database:
if ($result)
{
echo "Deleted Successfully";
}
else
{
echo mysql_error();
}
Second: the mysql_xxxx functions will be removed from PHP in future version. You should have a look at PDO to connect to your database
Syntax of your query has to be changed for deleting a row from table use following syntax
$sql = "DELETE FROM tablename WHERE id='$id'";
off topic, but please read http://php.net/manual/security.database.sql-injection.php
this type of query is vulnerable for SQL-Injections, because you don't check/quote your $id.
As a hint, these functions may help you:
mysql_real_escape_string
ctype_digit
is_numeric
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
Please im trying to insert data from a table to another. the issue is that i find duplicate records in database when applying the code below:
<?php
include ('lib/connexion.php');
$id_article = $_GET['num'];
$requete = "Select * from products where product_id=$id_article";
$resultats = mysql_query ($requete);
if($resultats === FALSE) {
die(mysql_error()); // TODO: better error handling
}
?>
<html>
<head>
<title>APP crud</title>
</head>
<body>
<?php
while ($ligne =mysql_fetch_array ($resultats)){
$sql2 ="INSERT INTO panier (product_title, description, prix)
VALUES ('".$ligne[1]."','".$ligne[2]."','".$ligne[3]."' ) ";
mysql_query ($sql2) or die ('Erreur : ' .mysql_error());
$resultats2 = mysql_query ($sql2);
if($resultats2 === FALSE) {
die(mysql_error()); // TODO: better error handling
}
header('Location: panier.php');
?>
Supprimer
Modifier
Ajouter
Retour
<?php } ?>
</body>
<html>
Can someone tell me why this happens? thanks.
Making this as a community wiki (I've nothing to gain here, or wanting to gain) and pulled from comments to close this with:
Because, you're doing this twice: mysql_query ($sql2)
"Remove this line mysql_query ($sql2) or die ('Erreur : ' .mysql_error()); – devpro"
and
"Mysql_ is deprecated use mysqli_* or PDO – devpro"*
and
"you could also ALTER your column(s) as UNIQUE. That will guarantee that no duplicates ever gets inserted."
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
I have the next code, but it inserts two rows in the mysql database instead of one. Could ypu please take a look to the code?
Regards.
<?php
$name= $_POST['name'];
$password = $_POST['password'];
mysql_connect("localhost","username","mypass");
mysql_select_db("databaseName");
mysql_query($query ="insert into users(name,password) values ('$name','$password')");
if (mysql_query($query) === TRUE) {
echo "Record saved";
} else {
echo "Error";
}
?>
Don't call mysql_query() when you assign the $query variable. And remember to escape your data, since you're not using prepared statements.
mysql_connect("localhost","username","mypass");
$name = mysql_real_escape_string($_POST['name']);
$password = mysql_real_escape_string($_POST['password']);
$query ="insert into users(name,password) values ('$name','$password')";
if (mysql_query($query)) {
echo "Record saved";
} else {
echo "Error: " . mysql_error();
}
Dont use the mysql_query function twice,
you want to check it in if statement, then dont call it before if clause.
See this following code.
$query ="insert into users(name,password) values ('$name','$password')"
if (mysql_query($query) === TRUE) {
echo "Record saved";
} else {
echo "Error";
}
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I know I am doing something wrong but I really would like to know what it is. I can echo the
username of the session loggedin user using <?php echo $_SESSION['username']; ?>but I don't know why it doesn't work when I try to query database using the same technique. my codes below
I include this in the page
<?php
session_start();
$username=$_SESSION['username'];
?>
and here is the code that was suppose to display firstname and user_id of the sessions logged in user
<?php
$conn = new mysqli('localhost', 'root', 'browser', 'test');
if (mysqli_connect_errno()) {
exit('Connect failed: '. mysqli_connect_error());
}
$username = '$username';
$sql = "SELECT `user_id`, `firstname` FROM `members` WHERE `username`='$username'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo '<br /> user_id: '. $row['user_id']. ' - firstname: '. $row['firstname'];
}
}
else {
echo '0 results';
}
$conn->close();
?>
$username = '$username';
PHP variables inside single-quotes are not expanded. So now your variable is the literal string '$username', which undoubtedly won't match any user in your database.
You probably need to set $username = $_SESSION['username']; in your second PHP script.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
help me with this code i am new to php
<?php
$conn=mysql_connect("localhost","root","","test");
if(isset($_POST['submit']))
{
$sql="INSERT INTO registration(fname,designation,emailid,
address,phonenumber)VALUES('".$_POST['fname']."','".$_POST['designation']."','".$_POST['ema
lid']."', '".$_POST['address']."','".$_POST['phonenumber']."')";
echo $sql;
$result=mysql_query($conn,$sql);
echo $result;
}
else{
echo "Error";
}
?>
its a registration page getting values and inserting it in the table...
You have the parameters around the wrong way here:
$result=mysql_query($conn,$sql);
Try
$result=mysql_query($sql, $conn) or die(mysql_error($conn));
Side notes:
Don't use mysql_*() functions: they're deprecated. Use mysqli_*() versions instead.
You should escape your user inputs with mysql_real_escape_string() to protect against SQL Injection attacks. Consider using prepared statements with mysqli_() instead.
Take a look at this link which is a good tutorial for inserting data (from a form etc.) to a mysql database.
Also: be aware of sql-injection and prevent it. here is a tutorial on how to do this: link
If you want to have readable code, set the $_POST[] values to a variable, and then pass them to the query, it's not different in fact but this is more easy and clean.:
<?php
$conn=mysql_connect("localhost","root","","test");
if(isset($_POST['submit']))
{
$fname = $_POST['fname'];
$designation = $_POST['designation'];
$emailid = $_POST['emailid'];
$address = $_POST['address'];
$phonenumber = $_POST['phonenumber'];
$sql="INSERT INTO registration(fname,designation,emailid,address,phonenumber)";
$sql .="VALUES('$fname', '$designation', '$emailid', '$address', '$phonenumber')";
echo $sql;
$result=mysql_query($conn,$sql);
echo $result;
}
else{
echo "Error";
}
?>
you hade a typing mistake in $_POST['emailid']...
and you can select your database with this:
mysql_select_db('your db name');
put this line after your connection variable means $conn
and this is wrong:
$result = mysql_query ($conn, $sql)
you have to set the query first:
$result = mysql_query($sql, $conn)
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
im tryting to look where did do wrong but i cant find the problem, i cant update my database, can someone see my code whats missing? Is always saying me "database error"
Code:
<?php
if(isset($_POST['enviar'])) {
$types="";
for ($i=0; $i<count($_POST['texames']);$i++)
$types=$types.$_POST['texames'][$i].',';
$meta_desc=$_POST['meta_desc'];
$meta_info=$_POST['meta_info'];
$id_meta=$_POST['id_meta'];
if($meta_desc && $meta_info) {
$sql="update metainfo set meta_desc, meta_info='$meta_desc', '$meta_info' where id_meta=$id_meta";
mysql_query($sql) or die("DAtabase Error ...");
header("Location: list.php");
} else {
echo '<script language="javascript">alert("Fill Form!");</script>';
}
}
?>
You are setting two columns simultaneously;
set meta_desc, meta_info='$meta_desc', '$meta_info' where
Change your query to:
update metainfo set meta_desc = '$meta_desc', meta_info = '$meta_info'
where id_meta = $id_meta
The problem with with your query :
update metainfo set meta_desc, meta_info='$meta_desc', '$meta_info'
should be
update metainfo set meta_desc='$meta_desc', meta_info='$meta_info'
you cannot set multiple columns simultaneously in the manner you were doing
There are quite a few things you need to consider changing in your code, but firstly try changing
mysql_query($sql) or die("DAtabase Error ...");
to
mysql_query($sql) or die("Database Error - " . mysql_error());
this will provide you with a proper error message.
You should also have a read about SQL Injection and consider updating your code to either mysqli or PDO
Try
$sql="update metainfo set meta_desc='$meta_desc', meta_info='$meta_info' where id_meta=$id_meta";
also if you echo out the error in your die statement youll have better debug info
your problem seesm to be invalid sql-syntax:
update metainfo set meta_desc, ...
you've messed up the field/value-syntax. there's no values for meta_desc and $meta_info isn't applied to a field, it should be like this:
update metainfo set meta_desc = '$meta_desc', meta_info = '$meta_info'
where id_meta = $id_meta
you are missing an "=" on your query after "meta_desc":
$sql="update metainfo set meta_desc=, meta_info='$meta_desc', '$meta_info' where id_meta=$id_meta";
and I'm not sure if it should be like the above or like this:
$sql="update metainfo set meta_desc='$meta_desc', meta_info='$meta_info' where id_meta=$id_meta";
You could alternatively try this:
$sql="UPDATE metainfo SET (meta_desc, meta_info) VALUES ('" . $meta_desc . "', '" . $meta_info . "') WHERE id_meta = " . $id_meta;