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 4 years ago.
Improve this question
I am trying to save multidimensional array in database using json_encode. if i echo json string its showing right output but in database string is changed after insert.
here is my code:
$email=$_POST['email'];
$watchlist=$_POST['watchlist'];
$watchshow=$_POST['watchshow'];
$yearshow=$_POST['yearshow'];
$quer = "SELECT email FROM users WHERE email = '$email'";
$q = mysqli_query($conn, $quer);
$count=0;
while($row = mysqli_fetch_array($q)){
$email = $row['email'];
$count++;
}
if($count==1) //if user already exist change greeting text to "Welcome Back"
{
$quer = "SELECT watchlist FROM users WHERE email = '$email'";
$q = mysqli_query($conn, $quer);
while($row = mysqli_fetch_array($q)){
$watch = $row['watchlist'];
}
$data = json_decode($watch, TRUE);
array_push($data,$watchlist);
$add=array();
array_push($add,$watchshow);
array_push($add,$yearshow);
$data[] = $add;
$t = json_encode($data , JSON_FORCE_OBJECT);
$sql = "update users set watchlist='$t' WHERE email='$email'";
if ($conn->query($sql) === TRUE) {
echo'updated';
} else {
echo'error';
}
}
else {
$new=array();
array_push($new,$watchlist);
$add=array();
array_push($add,$watchshow);
array_push($add,$yearshow);
$new[] = $add;
$name = json_encode($new);
$sql1 = "INSERT INTO users (email,watchlist)
VALUES ('$email','$name')";
if ($conn->query($sql1) === TRUE) {
echo 'success';
} else {
echo "Error: " . $sql1 . "<br>" . $conn->error;
}
}
if i echo $name output is
{"0":{"0":"Stranger Things","1":2017}}
but after insert it's showing this in database
{"0":"Stranger Things","1":{"0":"Stranger Things","1":"2017"}}
what i am doing wrong here?
You placed echo to unnecessary variable somewhere in your code thats why it prints name 2 times.. check it properly and remove it.
{"0":"Stranger Things","1":{"0":"Stranger Things","1":"2017"}}
This is happening most probably due to $new[] = $add; when you are using Loop and passing some value inside $new[i].
In first Loop its proper {"0":"Stranger Things","1":2017}
Now in second loop when i will be 1.
So, in position of 1 , {"0":"Stranger Things","1":2017} this is getting inserted again and making final array as {"0":"Stranger Things","1":{"0":"Stranger Things","1":"2017"}}
Show the complete code as it is, to identify the error.
As per the code you have provided, output must be correct.
$email= "abc#gmail.com";
$watchshow="Stranger Things";
$yearshow= 2017;
$new=array();
$add=array();
array_push($add,$watchshow);
array_push($add,$yearshow);
$new[] = $add;
$add=array();
array_push($add,$watchshow);
array_push($add,$yearshow);
$new[] = $add;
echo $name = json_encode($new, true);
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 6 months ago.
Improve this question
<?php
if (isset($_POST['submit'])) {
$title = $_POST['title'];
if (isset($_FILES['images']['name'])) {
$image_name = $_FILES['images']['name'];
echo($image_name);
$image_path = $_FILES['images']['tmp_name'];
echo($image_path);
$destination_path = 'location:'.SITEURL.'images/category/'.$image_name;
// echo($destination_path);
$upload = move_uploaded_file($image_name, $destination_path);
echo($upload);
if ($upload==0) {
$_SESSION['upload_image_category'] = "<div class='error'>failed to upload image </div>";
header('location:'.SITEURL.'admin/add-category.php');
die();
}
}
else{
$image_name = "";
}
if (isset($_POST['featureyon'])) {
$feature = $_POST['featureyon'];
}
else{
$feature = "No";
}
if (isset($_POST['a_yes'])) {
$active = $_POST['a_yes'];
}
else{
$active = "No";
}
# code...
$sql = "INSERT INTO tbl_category SET
title = '$title',
image_name='$image_name',
featured='$feature',
active='$active'
";
$res = mysqli_query($conn, $sql);
if ($res == True) {
$_SESSION['add_category'] = "<div class='success'>New category added</div>";
header('location:'.SITEURL.'admin/manage_category.php');
}
else{
$_SESSION['add_category'] = "<div class='error'>failed to add New category </div>";
header('location:'.SITEURL.'admin/add-category.php');
}
}
?>
the first
$destination_path should be a real pathname on the server, not a URL. Like this :- $destination_path = '../images/category/'.$image_name;
the second
move_uploaded_file(source_path, destination_path) Like this :-
$upload = move_uploaded_file($image_path, $destination_path);
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
<?php
$email = $_SESSION['eemailid'];
$query1 = mysql_query("SELECT * FROM attendance where email='$email'");
while($row = mysql_fetch_array($query1))
{
$status = $row['status'];
}
if ($status =='IN')
{
echo "Success";
}
else
{
echo "Failed";
}
?>
This is my phd code. I have a database named attendance in that i need to find a user my using email. After finding the user i have column called status in SQL. If the value of the status inside the column is "IN". I need to show Success. If the value is "OUT" i need to show Failed. here i am using email as session to find the current user. Can u please help me to do..
Thank You in advance.
Be sure you are connected to your database.
Be sure you have inserted correct data (username,password,ect.)
Please read this:
http://php.net/manual/en/function.mysql-connect.php
<?php
$link = mysql_connect('localhost', 'mysql_user', 'mysql_password');
if (!$link) {
die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully';
mysql_close($link);
?>
and then read this:
http://php.net/manual/en/function.mysql-query.php
<?php
$result = mysql_query("SELECT * FROM attendance where email='%s'"), mysql_real_escape_string($email));
if (!$result){
die('Invalid query: ' . mysql_error());
}
while($row = mysql_fetch_array($result)){
if ($row['status'] =='IN'){
echo "Success";
}
else {
echo "Failed";
}
}
?>
Try this way
<?php
$email = $_SESSION['eemailid'];
$result = mysql_query("SELECT * FROM attendance where email='"$email "'");
$status="";
while($row = mysql_fetch_array($result))
{
$status = $row['status'];
}
if ($status =='IN')
{
echo "Success";
}
else
{
echo "Failed";
}
?>
I'm not sure with your data in Database, but please double check follow these steps below:
$_SESSION['eemailid']; // eemailid or emailid? or email?
How many rows in your database with the column email equal to $email? What is the value of status column of the last row with email equal to $email? because your IF ELSE statement is out of WHILE loop
<?php
$email = $_SESSION['eemailid'];
$query1 = mysql_query("SELECT * FROM attendance where email='$email'");
while($row = mysql_fetch_array($query1))
{
$status = $row['status'];
}
if ($status =='IN')
{
echo "Success";
}
else if ($status == "OUT")
{
echo "Failed";
} else {
echo "Error"
}
?>
Use this code and tell us what happen. This could be a problem with your SQL query
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I am looking for PHP script that capable of imports my 3 column data into my table which has 5 fields.
Is this possible?
I think you need something like this
$file = fopen("airport 123.csv","r ");
$i = 0;
while(!feof($file))
{
$file_data = fgetcsv($file);
//print_r($file_data);
$select = "select count(*) from airport_pricing";
$result = mysql_num_rows($select);
if($result>0)
{
echo $sql = "UPDATE airport_pricing SET pickup ='$file_data[0]',dropoff='$file_data[1]', price='$file_data[2]' ";
}
else{
echo $sql = "INSERT INTO airport_pricing values(NUll,'$file_data[0]','$file_data[1]','$file_data[2]',1)";
}
$query = mysql_query($sql);
if(!$query)
{
$error = "Record is not inserted !";
}
else
{
$error = "Record is inserted Successfully !";
}
echo "<br />";
}
fclose($file);
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
<form action = "index.php" method = "post">
username : <input type = "text" name = "uname" /><br>
password : <input type = "text" name = "pass" /><br>
submit : <input type = "submit" name = "submit" value = "submit" />
</form>
<?php
if(isset($_SESSION['id'])){echo $_SESSION['id'];}
if(isset($_POST['submit'])){
if ($_POST['submit'] == 'submit'){
$uname = $_POST['uname'];
$pass = $_POST['pass'];
$db = "davidedwardcakes";
$connect = mysql_connect('localhost', 'root', 'wtfiwwu');
$db_connect = mysql_selectdb($db, $connect);
if(!$db_connect){echo 'no';}
$query = "SELECT * FROM `users` WHERE uname ='$uname' AND pass = '$pass'";
$result = mysql_query($query, $connect);
if(mysql_num_rows($result) > 0){//echo 'index failed'; var_dump($result);}
while($row = mysql_fetch_array($result)){echo $row['uname']
. "<br>";
session_start();
echo 'peruse';
$_SESSION['id'] = $row['id'];}}
else{echo 'lol'; var_dump($query);}}
Whenever I want to login, i get the error:
string 'SELECT * FROM users WHERE uname ='brown' AND pass = 'kenji'' (length=61)
meaning that theres a problem with my $query. If I remove the $pass query from $query it works fine but doesn't when it is included. Can anybody help please.
Let me convert your code to MySQLi at least. MySQL is already deprecated.
<?php
/* ESTABLISH CONNECTION */
$connect=mysqli_connect("YourHost","YourUsername","YourPassword","YourDatabase"); /* REPLACE NECESSARY DATA */
if(mysqli_connect_errno()){
echo "Error".mysqli_connect_error();
}
/* REPLACE THE NECESSARY POST DATA BELOW AND PRACTICE ESCAPING STRINGS BEFORE USING IT INTO A QUERY TO AVOID SOME SQL INJECTIONS */
$uname=mysqli_real_escape_string($connect,$_POST['username']);
$pass=mysqli_real_escape_string($connect,$_POST['password']);
$query = "SELECT * FROM `users` WHERE uname ='$uname' AND pass ='$pass'";
$result = mysqli_query($connect,$query); /* EXECUTE QUERY */
if(mysqli_num_rows($result)==0){
echo 'login failed';
var_dump($result);
}
else {
while($row = mysqli_fetch_array($result)){
echo $row['uname'];
} /* END OF WHILE LOOP */
echo 'Successfully Logged-in.';
var_dump($query);
} /* END OF ELSE */
?>
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 write this but it doesnt work, can`t find the error.
this server side code get the the variable $cpu & $display and use it in select from the data base. when the variable is not important "*" will be sent.
<?php
if (isset($_REQUEST['action']))
{
$action = $_REQUEST['action'];
}
else
{
echo "Invalid Data";
exit;
}
if ($action == "read")
{
readData();
}
function connectToDatabase()
{
$connection = mysqli_connect("localhost", "root", "", "project_pro");
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
return $connection;
}
function readData()
{
$connection = connectToDatabase();
$cpu = $_REQUEST['cpu'];
$display = $_REQUEST['display'];
This is the part that problem exists:
$sql = "Select * From phones WHERE";
if ($cpu == "*")
{
}
else
{
$sql+= " phone_cpu='$cpu'";
}
if ($display == "*")
{
}
else
{
$sql+= " AND phone_display='$display'";
}
$output = array();
while ($row = mysqli_fetch_array($result))
{
$record = array();
$record['phone_id'] = $row['phone_id'];
$record['phone_cpu'] = $row['phone_cpu'];
$output[] = $record;
}
echo json_encode($output);
mysqli_close($connection);
}
The concatenation operator in PHP is . not +. So change += to .=.