check if a value exists in my database table - php

I want to check if a URL exists in my MySQL database table for example if Url=exist, message=url already exist
<?php
if(isset($_POST['Submit'])){
$dbhost = 'localhost';
$dbuser = '####';
$dbpass = '#######';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
$Title = $_POST['Title'];
$Url = $_POST['Url'];
$email_data = $_POST['email_data'];
$type_data = $_POST['type_data'];
$sql = "INSERT INTO table ". "(Title,Url,email_data,type_data)"."VALUES('$Title','$Url','$email_data','$type_data')";
mysql_select_db('dbname');
$retval = mysql_query( $sql, $conn );
if(! $retval ){
die('Could not enter data: ' . mysql_error());
}
echo "<script type='text/javascript'>alert('submitted successfully!')</script>";
mysql_close($conn);
} else {
?>

You can do like this
$sql = "SELECT Url FROM 'your_table_name' WHERE Url = $_POST['Url']";
Run this sql and if it is return true you can say its exist.

$query = mysql_query('select url from table-name where url=$_post['url']');
if(mysql_fetch_rows($query) != 0){
echo "URL allready Exists";
}else{
Insert Query
}
Hi, Try this code.

Its a bit hard know what you are doing by just looking at the code but if you wondering how to check if something is in the database you could do like this:
PHP:
$sql = mysql_query("SELECT * FROM dbname WHERE Url='$Url'");
if(mysql_num_rows($sql) > 0){
echo "alreday exist";
}

Related

Query failed in php

$query = "SELECT * FROM Student WHERE student_id = $child AND school_id = $college";
$result = mysqli_query($conic, $query);
if(!$result){
die(" query failed");
echo 'error here';
} else {
i am trying to retrieve some information from the SQL database but the query seems to fail any ideas?
You must learn more : how to retrieve data from database using select in php
so try out following example to display all the records from Student table
<?php
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = 'rootpassword';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
die('Could not connect: ' . mysql_error());
}
$sql = "SELECT * FROM student WHERE student_id = '"$child"' AND school_id = '"$college"' ";
mysql_select_db('test_db');
$retval = mysql_query( $sql, $conn );
if(! $retval )
{
die('Could not get data: ' . mysql_error());
}
while($row = mysql_fetch_array($retval, MYSQL_ASSOC))
{
// some code here
}
echo "Fetched data successfully\n";
mysql_close($conn);
?>
NOTE
The mysql extension is deprecated and will be removed in the future:
use mysqli or PDO

PHP error: Query was empty

here is the code that gives me this error. I have searched around, found related questions, but I couldn't apply the implementation. Here is something wrong and it's beyond my understanding..
function add_user_to_db()
{
$dbhost = "111.111.111.111";
$dbuser = "Bob";
$dbpass = "password";
$connection = mysql_connect($dbhost, $dbuser, $dbpass);
if(!$connection){die('Could not connect: '.mysql_error());}
echo 'Connected to Vikings Game DB!';
mysql_select_db('vgDB');
$tb_result = mysql_query("SHOW TABLES LIKE 'Players'");
$table_exists = mysql_num_rows($tb_result) > 0;
if($table_exists)
{
if( isset($_POST["ID"]) && isset($_POST["NAME"]) && isset($_POST["COMMENT"]) )
{
$id = $_POST['ID'];
$name = $_POST["NAME"];
$comment = $_POST["COMMENT"];
//Check if a row exists
$row_result = mysql_query("SELECT `".$id."` FROM Players");
if($row_result == FALSE)
{
$add_user_query = mysql_query( "INSERT INTO Players( ID , NAME , COMMENT )VALUES('123','Bob','Bob's comment')" );
$retval = mysql_query($add_user_query, $connection);
if(!$retval) die("Could not insert data: ".mysql_error());
echo "User '".$id."' was added successfully!";
}
else
{
$name_read = mysql_query("SELECT NAME FROM Players WHERE ID = `".$id."`");
$comment_read = mysql_query("SELECT COMMENT FROM Players WHERE ID = `".$id."`");
echo "Reading data of user (`".$id."`): Name = `".$name_read."`; Comment: `".$comment_read."`";
}
}
}
mysql_close($connection);
}
I think your problem is in the following line:
$add_user_query = mysql_query( "INSERT INTO Players( ID , NAME , COMMENT )VALUES('123','Bob','Bob's comment')" );
Try modifing (just for testing purposes) as follows:
$add_user_query = mysql_query( "INSERT INTO Players( ID , NAME , COMMENT )VALUES('123','Bob','Bob comment')" );
If you get it working with this, means that 'Bob's comment' was the problem, the quote after the Bob was causing it.

syntax error when delete sql data using php

im getting the following syntax error can someone please help!
im guessing it something soooo easy but i have been looking at it for ages and can see what im doing wrong
<?php
if(isset($_POST['delete']))
{
$dbhost = '';
$dbuser = '';
$dbpass = '';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
die('Could not connect: ' . mysql_error());
}
$CourseId = $_POST['CourseId'];
$sql = "DELETE course ".
" WHERE CourseId = $CourseId" ;
mysql_select_db('d11os_projectdb');
$retval = mysql_query( $sql, $conn );
if(! $retval )
{
die('Could not delete data: ' . mysql_error());
}
echo "Deleted data successfully\n";
mysql_close($conn);
}
else
{
?>`enter code here`
$sql = "DELETE FROM course ". --<-- Missing key word FROM
" WHERE CourseId = $CourseId"
You are missing the table name from the sql query
$sql = "DELETE course FROM **table_name**".
" WHERE CourseId = $CourseId" ;

How to update many fields with single query?

I am working with a MLM company where com has to register the member, if it detects any mistake in data, it has to update the data of member again.
I have the right code which works well when all fields has to update but problem is it does not work for individual input boxes. Please give me solution of this problem.
<?php
if(isset($_POST['update'])) {
$dbhost = 'localhost';
$dbuser = 'vvvv';
$dbpass = 'xxxx';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn ) {
die('Could not connect: ' . mysql_error());
}
$usrid = $_POST['uid'];
$pwrd = $_POST['pwd'];
$nm = $_POST['noe'];
$fnm = $_POST['fn'];
$addrs = $_POST['adrs'];
$cntn = $_POST['cnt_no'];
$cty = $_POST['ct'];
$sql = "UPDATE office_user ".
"SET
password = '$pwrd',
name='$nm',
father_name='$fnm',
address='$addrs',
contact_no='$cntn',
city='$cty' ".
"WHERE user_id = '$usrid'" ;
mysql_select_db('my_db');
$retval = mysql_query( $sql, $conn );
if(! $retval ) {
die('Could not update data: ' . mysql_error());
}
echo "Updated data successfully\n";
mysql_close($conn);
}
?>

mysql update query is not running

MySQL update query is not working on the php based webpage also not showing any MySQL error but when copy the query and run it in SQL it works fine.
Here is my code:
$query = "UPDATE table_name SET page_name = '".$page_name."' WHERE ip = '".$ip."'";
$update = mysql_query($query) or die(mysql_error());
please if anyone sees anything wrong in the code please let me know.
You should try this syntax instead:
$query = "UPDATE table_name SET page_name = '$page_name' WHERE ip = '$ip'";
$update = mysql_query($query) or die (mysql_error());
Please note that mysql_* methods are deprecated and you should use mysqli_* methods instead.
Demo Code : Its Work Fine for me.Please Use it.
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
die('Could not connect: ' . mysql_error());
}
$page = 'Hello';
$ip = '102.101.22.23';
$sql = "UPDATE `pages`
SET `pagename` = '".$page."'
WHERE `ip` = '".$ip."'";
/* OR use $sql = "UPDATE pages SET pagename= '".$page."' WHERE ip='".$ip."'"; */
mysql_select_db('demo');
$retval = mysql_query( $sql, $conn );
if(! $retval )
{
die('Could not update data: ' . mysql_error());
}
echo "Updated data successfully\n";
mysql_close($conn);

Categories