Query with WHERE clause not working using mysqli bind_param() [closed] - php

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 2 years ago.
Improve this question
I am creating a webservice where I need to get some data from mysql and retrieve it in a mobile app.
I have been trying all day to do a query with a WHERE statement, but no success.
The normal query, without the WHERE is working perfectly though.
Also I went through all the similar questions on stack overflow and other forums, but none is the same problem as mine.
This is my current code:
<?php
$con=mysqli_connect("Hidden credentials");
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
if ($stmt = $con->prepare("SELECT
team.id as teamId,
team.team_name as teamName,
user.id as userId,
user.username as username,
team_members.role as role
FROM team_members
inner join user on team_members.user_id = user.id
inner join team on team_members.team_id = team.id
where user.id = ?
")) {
$stmt->bind_param("i", $id);
$stmt->execute();
$result = $stmt->get_result();
$resultArray = array();
$tempArray = array();
while($row = $result->fetch_object())
{
$tempArray = $row;
array_push($resultArray, $tempArray);
}
echo json_encode($resultArray);
$stmt->close();
}
?>
If I remove the line where user.id=? it gets the data correctly.
Running the query on phpMyAdmin is everything ok, so it's not any issue on the query.

The variable $id doesn't exist (in your code). If you want to set the variable dynamically you can use $id = $_GET['id'];. This will take the value from the url and put it in the variable!

Your QUERY doesn't work because your variable id doesn't exists (in code what you showing).
Fix: create variable id and put some data to this variable.
For example:
$id = 5;
Or dynamcially:
From URL with GET method:
$id = $_GET['id'];
this allows you to get parameter from URL. But you must set this parameter by link. For example: <a href="index.php?id=5">. By clicking on this a tag you will be redirected to page index.php with parameter id which equals to 5.
From POST method:
for example you have this FORM:
<form method="post">
<input type="number" name="id">
<input type="submit" name="submit">
</form>
after submiting this FORM values will be saved in $_POST. You can access them by $_POST["name"]. In this case $_POST["id"].
$id = $_POST["id"];
From SESSION:
$id = $_SESSION["id"];
but first you have define $_SESSION["id"]. You can access this variable ($_SESSION["id"]) in other pages of your domain.

Related

php getting variable outside if condition [closed]

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 6 years ago.
Improve this question
Hi i have little problem with if statements while checking if $GET values are sended. The problem is the following: If I run this code the variables in the second if condition becomes inserted but I cant get variables from the second if condition outside the second if condition. Why does that happening? I've already tried it with return but it wont work. What is the mistake?
$gameid = $_GET['gameid'];
$id = $_GET['id'];
$questionid = $_GET['questionid'];
if (isset($_GET['gameid'])) {
$answer = "answer_d";
}
if (isset($_GET['id'])) {
// insert values into mysql database
$answer = "answer_m";
}
then I want to echo the variable $answer in a <p> tag.
If I understand your question correctly then the issue is that the $answer variable doesn't exist outside of the scope of the if statements. Try defining $answer alongside your other variables such as $gameid and then updating the value within the if statements.
at first initialize the variable value. then you echo this variable after all condition then you understand which if block your code enter or not
$gameid = $_GET['gameid'];
$answer = '';
$id = $_GET['id'];
$questionid = $_GET['questionid'];
if (isset($_GET['gameid'])) {
$answer = "answer_d";
}
if (isset($_GET['id'])) {
// insert values into mysql database
$answer = "answer_m";
}

How do I display url [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I have a site in which people can click on names and will be redirected to the corresponding URL
id | name | url
1 | facebook | www.fb.com
I have 1,200,000,023 URL I want to display. URLs which loggedin user have not visited.
My signup information are stored in userdb.php like this:
1|username|a84894gf5sag4f684gh68fh45g|email#gmail.com|NA
My display.php is
<?php
mysql_connect('mysql', '1', 'Py');
mysql_select_db('a5803761_add');
$query =mysql_query('select * from addimage order by ID DESC');
while( $row = mysql_fetch_assoc($query) )
{
echo '<div style="min-width:300px;height:100px;border:red 5px;float:left;">'.$row['name'].'</div>';
}
?>
First, you have to store which user visited which url already. This table should have a structure like that:
CREATE TABLE VISITED(ID INT AUTO_INCREMENT,
USER_ID INT,
URL_ID INT,
PRIMARY KEY(ID),
KEY(USER_ID),
KEY(URL_ID));
Then you have to interprep the users' click to store if they visit an url (with jQuery maybe, and send back this with AJAX) or use a redirecter PHP which logs the clicked url and then redirect the user to this.
If you consider the second one, this PHP should be something:
<?php
//You should check here for the URL to be an integer
$res = mysql_query('SELECT url FROM addimage WHERE id='.$url_id.';');
if (mysql_num_rows($res)>0) {
mysql_query('INSERT INTO VISITED VALUES(NULL,'.$_SESSION['logged_in_user_id'].','.$url_id.');');
$row = mysql_fetch_array($res);
Header('Location: http://'.$row[0]);
}
?>
After that, when you list your URLs, you have to check which are the user didn't visit yet:
<?php
mysql_connect('mysql', '1', 'Py');
mysql_select_db('a5803761_add');
$sql = 'select addimage.id, addimage.name, VISITED.USER_ID from addimage ';
$sql .= 'LEFT JOIN VISITED ON (addimage.id = VISITED.URL_ID) ';
$sql .= 'WHERE (VISITED.USER_ID='.$_SESSION['logged_in_user_id'].');';
$query =mysql_query('order by ID DESC');
while( $row = mysql_fetch_assoc($query) )
{
if ($row[2] == 'NULL') {
echo '<div style="min-width:300px;height:100px;border:red 5px;float:left;">'.$row['name'].'</div>';
}
}
?>
But I think it's not a good idea to list such a lot of urls in one HTML, probably the browser will eat up all the memory. Maybe you should do some kind of filter or paging mechanins.
Please consider this code as an example only, and modify it for your needs.
On the other hand, as others suggested, DO NOT use mysql_ extensions, use mysqli or PDO.
I would advise you to use mysqli or PDO. O, and write "http://" into link.

No response to update query in oracle [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I am a beginner . I made a php html combined page(viewemployees.php) to display my table from oracle data base. It worked fine. Then i made a page to edit my data base table(editemployees.php). that page has two functions. It displays the original table and a simple form which should contain the elements to update the table. When we place values in the form it directs us to a new php page(een.php) where it updates table and returns back to editemployees.php showing the updated table. but i am stuck when i fill in the form AND NOTHING happens. Even I have placed a condition that to echo a line if new value field is null. still that line is not echoed. This is some weird thing. I am posting both of my edit and view pages code click the link. kindly help.
In simple my table update is not working and showing undefined variable error at line 4 of een.php. Why is taht variabl undefined?? can anyone help please
https://www.dropbox.com/sh/xtuvotdy7c9wr1v/AADrNSlC_EJ0YkyDDkhe8mKGa?dl=0
<?php
include("connection.php");
$empid = $_POST['EMPLOYEE ID'];
$field = $_POST['EDIT FIELD'];
$nfield = $_POST['NEW VALUE'];
echo $field;
if( empty($_POST['NEW VALUE'] )){
echo "type new field properly";
}
else
{
$e = filter_var($empid, FILTER_SANITIZE_EMAIL);
$f = filter_var($field, FILTER_SANITIZE_EMAIL);
$nf = filter_var($nfield, FILTER_SANITIZE_EMAIL);
}
if( $e==$empid && $f==$field && $nf==$nfield)
{
if ($field=="age" || $field=="sal"){
$sel = "seleect * from employ";
$st = oci_parse(conn, $sel);
oci_execute($st);
$query = " update employ set $field = $nfield where empid = $empid";
$stmt = oci_parse($conn,$query);
oci_execute($stmt, OCI_COMMIT_ON_SUCCESS);
oci_free_statement($updateTitleInserted);
oci_close($conn);
echo oci_error();
header("Location: home.html");
}
else{
$query = "update employ set $field = '$nfield' where empid = $empid";
$stmt = oci_parse($conn,$query);
oci_execute($stmt,OCI_COMMIT_ON_SUCCESS);
oci_free_statement($updateTitleInserted);
oci_close($conn);
echo oci_error();
echo $field;
}
}
else
echo "wrong data entry go back and enter again";
?>
This line does not make sense.
$empid = $_POST["EMPLOYER ID"];
A post variable name cannot contain a space.

PHPMyAdmin Query error in php #1064 [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
<?php
//GET FACEBOOK LIKE DATA TO FACEBOOK SERVERS
function readFacebookLikes($url) {
$query = "select like_count from link_stat WHERE url ='" . $url ."'";
$s = file_get_contents("https://api.facebook.com/method/fql.query?query=".
urlencode($query)."&format=json");
preg_match("#(\"like_count\"):([0-9]*)#",$s,$ar);
if(isset($ar[2])) return $ar[2]; else return null;
}
mysql_connect('localhost','root','') or die(mysql_error());
mysql_select_db('odrinhasedm') or die(mysql_error());
$query=mysql_query("select nome, url from participacao") or die(mysql_error());
while($res=mysql_fetch_array($query))
{
$likes = "http://[ip]/evento-odrinhas-edm-open-air/".$res['url'];
$links = $res['url'];
$contador = readFacebookLikes($likes);
//echo'<tr><td>'.$res['nome'].'</td><td>'.$contador.'</td></tr>';
$query1 = "UPDATE participacao set likes = $contador WHERE url = $links ";
mysql_query($query1) or die (mysql_error());
}
?>
Why is this query1 not working? (PHPMyAdmin errror #1064)
I have a project that creates a page per user on a form and each page has its own Facebook like system, now I have a way to get that data and show it on a table, but I want to insert that data into my database to sort it. Is it easier to add the values to my db or sort the table directly in php?
I have a query that is working and its pulling the URLs to the PHP file, then I want to create another query to insert the data into the database with "update".
I think url is a string. So you must have quotes around the values:
UPDATE participacao set likes = $contador WHERE url = '$links'

HTML Form sending data to SQL table [closed]

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 9 years ago.
Improve this question
When submitted, a new row gets added to the 'servers' table, but every data says '0' instead of the data I inputted.. I'm using VARCHAR64 for the rows..
Form.html
<html>
<head>
</head>
<body>
<form action="db.php" method="post">
server id: <input type="text" name="post_serverid">
server ip: <input type="text" name="post_serverip">
<input type="submit>
</body>
</html>
db.php
<?php
$con=mysqli_connect("localhost","root","","toplist");
//Checking the connection
if(mysqli_connect_errno($con))
{
echo "Cold not connect to SQL Database: " + mysqli_connect_error();
}
mysqli_query($con, "INSERT INTO servers (serverid, serverip) VALUES ('$_POST['post_serverid']', '$_POST['post_serverip']')");
?>
First: NEVER just throw user data straight into your database without validating / cleansing it first.
Next, you've got some odd punctuation going on in your mysqli_query(). Try this:
$serverid = $mysqli->real_escape_string($_POST['serverid']);
$serverip = $mysqli->real_escape_string($_POST['serverip']);
$sql = "INSERT INTO servers (serverid, serverip) VALUES('$serverid', '$serverip');";
mysqli_query($con, $sql);
From OP's comment:
"Strangely enough, mysqli_query($con,"INSERT INTO form (name, dob) VALUES ('$_POST[post_name]', '$_POST[post_dob]')"); works."
"But when I replace NAME with SERVERID, and replace DOB with SERVERIP, the inputted data doesn't insert."
The problem is, you need to change your POST variables and column names accordingly.
Rename your NAME column to SERVERID and DOB column to SERVERIP.
and post_name to serverid and post_dob to serverip
The new query can now be done this way:
$serverid = mysqli_real_escape_string($con,$_POST['serverid']);
$serverip = mysqli_real_escape_string($con,$_POST['serverip']);
$sql = "INSERT INTO `servers` (`serverid`, `serverip`) VALUES ('$serverid', '$serverip');";
mysqli_query($con, $sql);
Using this method will help prevent against SQL injection.
You might want to use $_POST['key'] instead of $_POST[key]
$_POST is an associative array, you need to access the keys with single or double quotes.
Example:
change your query to
$query = "INSERT INTO servers (serverid, serverip) VALUES ('".$_POST['post_serverid']."','".$_POST['post_serverip']."')";
mysqli_query($con, $query);
Also, you are currently volunerable to SQL injection. I'd suggest to use a prepared statement or at least escape the $_POST variables before inserting it to the DB.
Hope this helps!
$serverip=$_POST['post_serverid'];
$serverid=$_POST['post_serverip'];
$query = mysqli_query($con,"INSERT INTO servers (serverid, serverip) VALUES ('$serverid','$serverid')");
Try this, it should work

Categories