How to get notification when insert new value in db? - php

Hi i am inserting value in data base in php i want that when i insert value in database then my div color should be change
insert.php
include('conn.php');
if(! $conn )
{
die('Could not connect: ' . mysql_error());
}
$id=2;
//$sql="insert into messages (message,number,service) values ('dd',".$urlstring.",'ds')";
$sql = 'INSERT INTO messages '.
'(message,number,service) '.
'VALUES ( "'.$message.'", "'.$urlstring.'", "'.$service.'" )';
$retval = mysql_query( $sql, $conn );
if(! $retval )
{
die('Could not enter data: ' . mysql_error());
}
echo ("Records affcted: ". mysql_affected_rows());
//echo "Entered data successfully\n";
mysql_close($conn);
index.php
<div id="div1" style="height=200px;width=300px;">
</div>
here i want when insert.php execute then div section should change color in another file
How can i achieve this
Any help will be appreciated

You can use timer on javascript and create a ajax function to retrieve all inserted data on the database.

try this:
include('conn.php');
if(! $conn )
{
die('Could not connect: ' . mysql_error());
}
$id=2;
//$sql="insert into messages (message,number,service) values ('dd',".$urlstring.",'ds')";
$sql = 'INSERT INTO messages '.
'(message,number,service) '.
'VALUES ( "'.$message.'", "'.$urlstring.'", "'.$service.'" )';
$retval = mysql_query( $sql, $conn );
if(! $retval )
{
die('Could not enter data: ' . mysql_error());
}
echo ("Records affcted: ". mysql_affected_rows());
//echo "Entered data successfully\n";
?>
<style>
#div1{
backgroud-color="yourcolor";
}
</style>
<?php
mysql_close($conn);
ah i see.
maybe this can help you: http://www.barelyfitz.com/projects/csscolor/?

Related

How to use Insert query in PHP code

I do not understand what is going wrong with code. The result is get is "connected successfully success Query failed". I tried few combinations and I get the same result. Please help me in solving this. Thanks in advance.
<?php
$link = mysql_connect('localhost', 'root1', '')
or die('Could not connect: ' . mysql_error());
if ($link) {
echo 'connected successfully';
}
$l = mysql_select_db('vtflix', $link) or die ('Could not select the database');
if ($l) {
echo ' success';
}
/*$varCNAME = 'John';
$varCONTENT = '4';
$varVID = '1';*/
$sql = "INSERT INTO mpaa(C_Name, ContentRating, V_ID) VALUES ('Jon', 4, 3)";
mysql_query($sql, $link) or die("Query failed");
$que = "SELECT * FROM mpaa";
$query = mysql_query($que, $link);
if (!$query) {
echo 'query failed';
}
while ($sqlrow = mysql_fetch_array($query, MYSQL_ASSOC)) {
$row = $sqlrow['C_Name'];
$nrow = $sqlrow['Content Rating'];
$mrow = $sqlrow['V_ID'];
echo "<br>" . $row . " " . $nrow . " " . $mrow . "<br>";
}
mysql_close($link);
?>
1.Don't use mysql_* library (deprecated from php5 onward + removed from php7) .Use mysqli_* OR PDO.
2.An example of mysqli_*(with your code)is given below:-
<?php
error_reporting(E_ALL); // check all type of error
ini_set('display_errors',1); // display those errors
$link = mysqli_connect('localhost', 'root1', '','vtflix');
if($link){
echo 'connected successfully';
$sql= "INSERT INTO mpaa(C_Name,ContentRating,V_ID) VALUES ('Jon', 4, 3)";
if(mysqli_query($link,$sql)){
$query = "SELECT * FROM mpaa";
$res = mysqli_query($link,$query);
if($res){
while($sqlrow=mysqli_fetch_assoc($query))
{
$row= $sqlrow['C_Name'];
$nrow= $sqlrow['Content Rating'];
$mrow= $sqlrow['V_ID'];
echo "<br>".$row." ".$nrow." ".$mrow."<br>";
}
mysqli_close($link);
}else{
echo die('Query error: ' . mysqli_error($link));
}
}else{
echo die('Query error: ' . mysqli_error($link));
}
}else{
echo die('Could not connect: ' . mysqli_connect_error());
}
?>
Note:- To check php version (either on localhost or on live server) create a file with name phpInfo.php, and just write one line code in that file:-
<?php
phpinfo();
?>
Now run this file and you will get the current php version.
Like this:- https://eval.in/684551
Here it seems that you are using deprecated API of mysql_* .
1) Check your PHP version
<?php phpinfo();exit;//check version ?>
2) avoid the usage of mysql use mysqli or PDO
3) change your db connection string with this :
new Mysqlidb($hostname, $username, $pwd, $dbname);
example with you code
<?php
$link = mysqli_connect('localhost', 'root1', '','vtflix');
if($link){
echo 'connected successfully';
$sql= "INSERT INTO mpaa(C_Name,ContentRating,V_ID) VALUES ('Jon', 4, 3)";
if(mysqli_query($link,$sql)){
$query = "SELECT * FROM mpaa";
$res = mysqli_query($link,$query);
if($res){
while($sqlrow=mysqli_fetch_assoc($query))
{
$row= $sqlrow['C_Name'];
$nrow= $sqlrow['Content Rating'];
$mrow= $sqlrow['V_ID'];
echo "<br>".$row." ".$nrow." ".$mrow."<br>";
}
mysqli_close($link);
}else{
echo die('Query error: ' . mysqli_error($link));
}
}else{
echo die('Query error: ' . mysqli_error($link));
}
}else{
echo die('Could not connect: ' . mysqli_connect_error());
}
?>

error in image_address and name insertion in mysql

i use this code for insert data in mysql but it directly shows error
" Could not enter data: "......
else
{
$image=$_FILES['img']['name'];
$tmp_name=$_FILES['img']['tmp_name'];
$path= "/var/www/html/uploads/".$image;
if(move_uploaded_file($name,$path))
{
$sql = "INSERT INTO form1 ". "(fname,lname,username,password,age,email,branch,college,
gender,image_p,) ". "VALUES('$fname','$lname','$username','$password','$age','$mail','$branch','$college','$gender','$image')";
$retval = mysql_query( $sql, $conn );
}
if(! $retval )
{
die('Could not enter data: ' . mysql_error());
}
else
{
echo "Entered data successfully\n"."\n ";
echo "you are now a registered user.";
mysql_close($conn);
}
$sql = "INSERT INTO form1 (fname,lname,username,password,age,email,branch,college,
gender,image_p) VALUES ('".$fname."','".$lname."','".$username."','".$password."','".$age."','".$mail."','".$branch."','".$college."','".$gender."','".$image."')";
$retval = mysql_query( $sql, $conn );
$image=$_FILES['img']['name'];
$tmp_name=$_FILES['img']['tmp_name'];
$path= "/var/www/html/uploads/".$image;
move_uploaded_file($name,$path);
if(! $retval ){
die('Could not enter data: ' . mysql_error());
}else{
echo "Entered data successfully\n"."\n ";
echo "you are now a registered user.";
mysql_close($conn);
}

Why am I getting this error "Error: Query was empty"

I am trying to update my SQL database using a form through php, but i keep getting the error "Error: Query was empty".
<?php
$sql = "";
$con = mysql_connect("*******","*******");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("*******", $con);
mysql_query($sql, $con);
if (isset($_POST['STUDENT_FNAME'], $_POST['STUDENT_SNAME'],
$_POST['STUDENTNO'] ))
{
$sql="UPDATE STUDENT SET STUDENT_FNAME=('$_POST[STUDENT_FNAME]'),
STUDENT_SNAME=('$_POST[STUDENT_SNAME]')
WHERE STUDENTNO=
('$_POST[STUDENTNO]')";
}
if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
echo "1 record updated";
mysql_close($con);
?>
It also won't update my table and I don't know what I've done wrong. All help will be much appreciated. I am new to this as you can probably tell!
There is an error in your code first you are calling mysql_query($sql, $con); without any query in your $sql variable your $sql is blank ""
<?php
$sql = "";
$con = mysql_connect("*******","*******");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("*******", $con);
if (isset($_POST['STUDENT_FNAME'], $_POST['STUDENT_SNAME'],
$_POST['STUDENTNO'] ))
{
$sql="UPDATE STUDENT SET STUDENT_FNAME=('$_POST[STUDENT_FNAME]'),
STUDENT_SNAME=('$_POST[STUDENT_SNAME]')
WHERE STUDENTNO=
('$_POST[STUDENTNO]')";
}
if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
echo "1 record updated";
mysql_close($con);
?>
Remove mysql_query($sql, $con) query execution after db selection because $sql is empty,
Also put your update sql execution in IF conditions, because if its not true than again $sql will be empty and you will get same error again,...
<?php
$sql = "";
$con = mysql_connect("*******","*******");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("*******", $con);
// mysql_query($sql, $con); // <-- remove this
if (isset($_POST['STUDENT_FNAME'], $_POST['STUDENT_SNAME'],
$_POST['STUDENTNO'] ))
{
$sql="UPDATE STUDENT SET STUDENT_FNAME=('$_POST[STUDENT_FNAME]'),
STUDENT_SNAME=('$_POST[STUDENT_SNAME]')
WHERE STUDENTNO=
('$_POST[STUDENTNO]')";
if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
echo "1 record updated";
}
mysql_close($con);
?>

Inserting a row into a table using PHP MYSQL

The following php code is meant to drop a table if it exists, create the table, use the table, and then insert a row into the table.
Everything works apart from the insert. I am new to PHP and MYSQL and I have tried many permutations of different types of quotes (singles, doubles, this one: `) but cannot get the data to be inserted into the table.
Can anybody shed some light on what is wrong with this?
$retval = mysqli_query($conn,'INSERT INTO `performance` (manager, program, programid, yearmonth, performance) VALUES ("manager1", "program1","programid1", "199901", "-3.4")');
The php script below gives the output:
Connected successfully
Table dropped successfully.
DB used successfully.
Table created successfully.
Could not insert data.
So everything worked apart from the insert.
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully<br />';
$retval = mysql_query('DROP TABLE IF EXISTS `managedfutures`.`performance`') or die(mysql_error());
if(! $retval )
{
die('Could not drop table ' . mysql_error());
}
echo "Table dropped successfully.";
echo "<br>";
$retval = mysql_query("USE managedfutures", $conn);
if(! $retval )
{
die('Could not use DB' . mysql_error());
}
echo "DB used successfully.";
echo "<br>";
$sql = "CREATE TABLE performance( ".
"performance_id INT NOT NULL AUTO_INCREMENT, ".
"manager VARCHAR(255) NOT NULL, ".
"program VARCHAR(255) NOT NULL, ".
"programid VARCHAR(255) NOT NULL, ".
"yearmonth VARCHAR(6) NOT NULL, ".
"performance FLOAT NOT NULL, ".
"PRIMARY KEY (performance_id )); ";
$retval = mysql_query( $sql, $conn );
if(! $retval )
{
die('Could not create table: ' . mysql_error());
}
echo "Table created successfully.";
echo "<br>";
$retval = mysqli_query($conn,'INSERT INTO `performance` (manager, program, programid, yearmonth, performance) VALUES ("manager1", "program1","programid1", "199901", "-3.4")');
if(! $retval )
{
die('Could not insert data. ' . mysql_error());
}
echo "Data inserted successfully.";
echo "<br>";
return;
Thanks to Mike W for pointing out that I had mixed mysql and mysqli commands! I am new to php/mysql and did not realise that there was a difference between the two. There was another error also, I was inputting a number as a string in the insert statement. I.e. I wrote "-3.4" instead of just -3.4.
For completeness, here is the fixed version which works.
$mysqli = new mysqli($dbhost, $dbuser, $dbpass, $dbname);
if ($mysqli->connect_errno) {
die("Failed to connect to MySQL: " . $mysqli->connect_error);
}
echo 'Connected successfully<br />';
$retval = mysqli_query($mysqli,"DROP TABLE IF EXISTS `performance`");
if(! $retval )
{
die('Could not drop table ' . $mysqli->query_error);
}
echo "Table dropped successfully.";
echo "<br>";
$sql = "CREATE TABLE performance( ".
"performance_id INT NOT NULL AUTO_INCREMENT, ".
"manager VARCHAR(255) NOT NULL, ".
"program VARCHAR(255) NOT NULL, ".
"programid VARCHAR(255) NOT NULL, ".
"yearmonth VARCHAR(6) NOT NULL, ".
"performance FLOAT NOT NULL, ".
"PRIMARY KEY (performance_id )); ";
$retval = mysqli_query($mysqli, $sql);
if(! $retval )
{
die('Could not create table: ' . $mysqli->query_error);
}
echo "Table created successfully.";
echo "<br>";
$retval = mysqli_query($mysqli, "INSERT INTO `performance` (`manager`, `program`,`programid`, `yearmonth`, `performance`) VALUES ('manager1', 'program1','programid1', '199901', -3.4)");
if(! $retval )
{
die('Could not insert data. ' . $mysqli->query_error);
}
echo "Data inserted successfully.";
echo "<br>";
return;
You're mixing mysql_*() and mysqli_*() calls. The two are different and cannot be used together. mysql_*() is deprecated - use only mysqli_*().
You have used mysql_query throughout your code
$retval = mysql_query( $sql, $conn ); //**You have used mysql_query**
if(! $retval )
{
die('Could not create table: ' . mysql_error());
}
echo "Table created successfully.";
echo "<br>";
Suddenly a mysqli_query is seen (MAGIC !!!).
$retval = mysqli_query($conn,'INSERT INTO `performance` (manager, program, programid, yearmonth, performance) VALUES ("manager1", "program1","programid1", "199901", "-3.4")');
^^
// SUDDENLY you see mysqli_query

php mysql query string

I need help to query string from database please help.
<?php
$phone="8165526693#vtext.com";
$link = mysql_connect('localhost', 'root', 'toor');
if (!$link) {
die('Could not connect: ' . mysql_error());
}
if (!mysql_select_db('wizarddb')) {
die('Could not select database: ' . mysql_error());
}
$result = mysql_query("SELECT * FROM Phone WHERE phone LIKE '%$phone%'");
if (!$result) {
die('Could not query:' . mysql_error());
}
echo mysql_result($result(1); // outputs phone
mysql_close($link);
?>
I have issues with echo or is the query wrong?
Your error is on the echo.
echo mysql_result($result(1); // outputs phone
That SHOULD be
echo mysql_result($result[1]); // outputs phone

Categories