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);
}
Related
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());
}
?>
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);
?>
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/?
I've already asked a question on this code I'm working on, just not about the same problem. Either way sorry for the repost!
So I'm having trouble with the code, as follows:
<?php
// Create connection
$host = "localhost";
$username="tudor";
$password="passw0rd";
$con=mysqli_connect($host, $username, $password);
if(! $con )
{
die('Could not connect: ' . mysqli_error());
}
echo 'Connected successfully<br />';
$db_1 = mysqli_select_db( $con, 'db_1' );
if (! $db_1) {
die('Could not select database: ' . mysqli_error());
}
else {
echo "Database successfully selected<br />===============================<br />";
}
//===================================
$a = 1;
$b = 2234;
$table = "CREATE TABLE info (id INT NOT NULL AUTO_INCREMENT, city CHAR(40), country CHAR(40))";
if (! $table) {
die('Could not create table ' . mysqli_error($con));
}
else {
echo "Table created<br />";
}
$insert = "INSERT INTO info (city, country) VALUES ($a, $b)";
if (! $insert) {
die('Could not insert ' . mysqli_error($con));
}
else {
echo "Inserted<br />";
}
$select = "SELECT * FROM info";
$result = mysqli_query ($con, $insert);
if (! $result) {
die('Result not working ' . mysqli_error($con));
}
else {
echo "Result working<br />";
}
echo "result: ".$result['city']. " ";
mysqli_close($con);
?>
This outputs (blockquote doesn't display page breaks):
Connected successfully Database successfully selected
=============================== Table created Inserted Result not working Table 'db_1.info' doesn't exist
What does it mean by "Table 'db.info'" not existing? It clearly says that my info table was created...
What I tried doing is inverting the variables in the $result query: $result = mysqli_query ($insert, $con);, because I had seen that syntax in a book. However all it gave was the following message in the output:
Warning: mysqli_query() expects parameter 1 to be mysqli, string given
in C:\wamp...
Thoughts anyone? Thanks in advance!
Edit: really appreciate the help everyone, thanks a lot!
You are not doing a mysqli_query() on $table before your mysqli_query() on $insert, and you are not doing a mysqli_query() on $select
$table = "CREATE TABLE info (id INT NOT NULL AUTO_INCREMENT, city CHAR(40), country CHAR(40))";
if (! $table)
$insert = "INSERT INTO info (city, country) VALUES ($a, $b)";
if (! $insert) {
$select = "SELECT * FROM info";
$result = mysqli_query ($con, $insert);
if (! $result)
try adding the mysqli_query() -
$table_sql = "CREATE TABLE `info` (`id` INT NOT NULL AUTO_INCREMENT, `city` CHAR(40), `country` CHAR(40), PRIMARY KEY (`id`))";
$table = mysqli_query ($con, $table_sql);
if (! $table) {
die('Could not create table ' . mysqli_error($con));
}
else {
echo "Table created<br />";
}
$insert_sql = "INSERT INTO `info` (`city`, `country`) VALUES ('$a', '$b')";
$insert = mysqli_query ($con, $insert_sql);
if (! $insert) {
die('Could not insert ' . mysqli_error($con));
}
else {
echo "Inserted<br />";
}
$select = "SELECT * FROM `info`";
$result = mysqli_query ($con, $select);
if (! $result) {
die('Result not working ' . mysqli_error($con));
}
else {
echo "Result working<br />";
}
Edit
Also, this line will fail -
echo "result: ".$result['city']. " ";
as you have to fetch the array from the query using mysqli_fetch_array()
$results = mysqli_fetch_array($result);
echo "result: ".$results['city']. " ";
Table 'db_1.info' doesn't exist
Means, that table info does not exist in db db_1 so, make sure if that is the case.
ok. Here is your code.
if(! $con )
{
die('Could not connect: ' . mysqli_error());
}
echo 'Connected successfully<br />';
if (!$con) {trigger_error("Could not connect to MySQL: " . mysqli_connect_error()); }
else { echo "Database successfully connected<br />===============================<br />"; }
$a = 1;
$b = 2234;
$table = mysqli_query($con,"CREATE TABLE IF NOT EXISTS info (`id` int(11) unsigned NOT NULL auto_increment,
`city` CHAR(40),
`country` CHAR(40), PRIMARY KEY (`id`) )ENGINE=MyISAM DEFAULT CHARSET=utf8");
if (!$table) {
die('Could not create table ' . mysqli_error($con));
}
else {
echo "Table created<br />";
}
$insert = mysqli_query ($con,"INSERT INTO info (city, country) VALUES ('$a', '$b')");
if (!$insert) {
die('Could not insert ' . mysqli_error($con));
}
else {
echo "Inserted<br />";
}
$select = mysqli_query ($con,"SELECT * FROM info");
$res=mysqli_fetch_array($select);
if (! $res) {
die('Result not working ' . mysqli_error($con));
}
else {
echo "Result working<br />";
}
echo "result: ".$res['city']. " ";
echo "result: ".$res['country']. " ";
mysqli_close($con);
actually i want to send a comment to the each image and it should display just after clicking the button . I am able to do insert and retrieve the comment but it require refresh the page and i don't want to refresh....just like orkut.plz help me i m new in php...
thans to all............
insertimg.php
//________________________________________FOR INSERT COMMENT_____________________________________________________
if (isset($_POST['Submit']))
{
$sql = "INSERT INTO comment(imid, comm) values ('".mysql_real_escape_string(stripslashes($_REQUEST['imgId']))."', '".mysql_real_escape_string(stripslashes($_REQUEST['Comment']))."')";
//$sql = "INSERT INTO comment (com) VALUES ($_POST['Comment'])";
//$sql="UPDATE upload SET comm='$_REQUEST['Comment']'WHERE id='$_REQUEST['imgId']'";
if($result = mysql_query($sql ,$conn))
{
echo "submited:";
}
else
{
echo "<h1>problem </h1> ".mysql_error();
}
}
For display comment..
$page=$_GET["page"];
$sql = "select comm from comment where imid = '".$page."'";
$retval = mysql_query( $sql, $conn );
if(! $retval )
{
die('Could not get data: ' . mysql_error());
}
echo"Comments:";
echo "<br>";
echo "<br>";
while($row = mysql_fetch_array($retval, MYSQL_ASSOC))
{
//echo $row['comm'];
echo "<textarea name=\"Comment\" style=\"background-color:#81F7BE;\">"; echo $row['comm']; echo "</textarea>";
//echo "<font>";
echo "<br>";
echo "<br>";
}
mysql_close($conn);
?>
You can solve it with AJAX.
You can use something like jQuery Ajax library.
$.ajax({
type: "POST",
url: "inserting.php",
data: "imid=1&comm=Hi",
success: function(msg){
alert( "Ajax Response: " + msg );
}
});