Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
This is my code
I have 2 table
booking // create to keep the booking from member
userinfo //keep username of member
<?php
$ta = $_POST['table'];
$day=$_POST['date'];
$usern = $_SESSION['username'];
$con = mysql_connect("localhost","root","password") or die ("can't connect to host");
mysql_select_db("register",$con) or die("can't connect to database");
$sql = "select * from userinfo where username = '$usern' ";
$rs = mysql_query($sql);
$row = mysql_num_rows($rs);
if($row == 0)
{ mysql_query("insert into booking(username) values '".$usern. "' ");
mysql_db_query("register");
} mysql_close();
?>
i don't know why it doesn't update.
You're missing your parenthesis in your INSERT statement:
"insert into booking(username) values '".$usern. "' "
should be
"insert into booking(username) values ('".$usern. "') "
First things first: mysql is becoming unsupported. Use Mysqli instead. It's not much different...
Then, the issue you have is to leave out the "_db". It's just mysql_query(your request). Plus, the second statement doesn't need to exist.
Need error handling? Include (before ;) "or die (mysql_error())
Related
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 1 year ago.
Improve this question
I need PHP code to detect is that line in MySQL database table.
Thanks
from my understanding of your question - in your php code you can do something like this
<?php
$con=mysqli_connect("localhost","my_user","my_password","my_db");
$sqlSearch = "select everything from <table_name> where <column_name> = <value>;";
$result = mysqli_query($con,$sqlSearch );
if (mysqli_num_rows($result) > 0){
// do nothing
}
else{
$sqlInsert = "insert into <table_name> (column1,column2...) VALUES(value1,value2...);";
$result = mysqli_query($con,$sqlInsert);
}
?>
basically, you want to search the database to see if that row exist and if it does not you want to perform an insert query.
I hope this piece of code, helps you.
<?php
$variable1 = 1;
$query = "SELECT * FROM table_name WHERE column_name='$variable1'";
$result = $conn->query($query); //$conn is the connection variable
if($result->num_rows==0){
//insert into table if no duplicate tuple doesnot exist.
$insertquery = "INSERT INTO table_name (column_name) VALUES ('$variable1')";
$insertresult = $conn->query($insertquery);
}else{
//do something
}
?>
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 6 years ago.
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.
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.
Improve this question
I have a query that im certain is correct but alas it is not. and that being said i am at a loss as to what could be causing the issue. here is my php script.
$con = mysqli_connect($DB_HOST,$DB_USER,$DB_PASSWORD,$DB_DATABASE);
if(!$con){
echo "Connection Error...".mysqli_connect_error();
}
else
{
echo "Database connection Success...";
}
$user_name = $_POST["login_name"];
$user_pass = $_POST["login_pass"];
$sql_query = "SELECT name from user_info where user_name like '$user_name'
and user_pass like '$user_pass'";
$result = mysqli_query($con,$sql_query);
if(mysqli_num_rows($result)>0)
{
$row = mysqli_fetch_assoc($result);
$name = $row ["name"];
echo "Hello welcome".$name;
}
else {
echo "No user found";
}
?>
You haven't hadded quotes! Remember $query is a string!
$con = mysqli_connect($DB_HOST,$DB_USER,$DB_PASSWORD,$DB_DATABASE);
if(!$con) {
echo "Connection Error...".mysqli_connect_error();
} else {
echo "Database connection Success...";
}
$user_name = $_POST["login_name"];
$user_pass = $_POST["login_pass"];
$sql_query = "SELECT name from user_info WHERE user_name like '$user_name'
and user_pass like '$user_pass'";
$result = mysqli_query($con,$sql_query);
if(mysqli_num_rows($result)>0) {
$row = mysqli_fetch_assoc($result);
$name = $row ["name"];
echo "Hello welcome".$name;
} else {
echo "No user found";
}
You forgot to wrap your query string in quotes.
You are writing query in your PHP variable so it must be wrapped in quotes.
$sql_query = "SELECT name from user_info where user_name like '{$user_name}' and user_pass like '{$user_pass'}";
What kind of error are you experiencing?
Your query should be a string, which means you'll need to surround it with quotes when assigning it:
$sql_query = "SELECT name from user_info where user_name like '" . $user_name
. "' and user_pass like '" . $user_pass . "'";
you can also use this syntax:
$sql_query = "SELECT name from user_info where user_name like '{$user_name}' and user_pass like '{$user_pass}'";
This will, however leave you open to SQL Injection Attacks.
See this part of the PHP documentation for more.
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
someone please help me to set mysql database connection variable on below php scripts but I couldn't fixed it.
$sql = "SELECT DISTINCT r.itemID, r2.ratingValue - r.ratingValue
as rating_difference
FROM rating r, rating r2
WHERE r.userID=$userID AND
r2.itemID=$itemID AND
r2.userID=$userID;";
$db_result = mysql_query($sql, $connection);
$num_rows = mysql_num_rows($db_result);
while ($row = mysql_fetch_assoc($db_result)) {
$other_itemID = $row["itemID"];
$rating_difference = $row["rating_difference"];
if (mysql_num_rows(mysql_query("SELECT itemID1
FROM dev WHERE itemID1=$itemID AND itemID2=$other_itemID",
$connection)) > 0) {
$sql = "UPDATE dev SET count=count+1,
sum=sum+$rating_difference WHERE itemID1=$itemID
AND itemID2=$other_itemID";
mysql_query($sql, $connection);
if ($itemID != $other_itemID) {
$sql = "UPDATE dev SET count=count+1,
sum=sum-$rating_difference
WHERE (itemID1=$other_itemID AND itemID2=$itemID)";
mysql_query($sql, $connection);
}
}
You didn't connect to any database use some thing like this
$link = mysql_connect('dbhost', 'dbusername', 'dbpassword');
$connection = mysql_select_db('tablename', $link);
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 9 years ago.
Improve this question
NOTE: Question Closed. I managed to figure out what was wrong.
My current code is this:
<?php
$con=mysqli_connect("HOST","USERNAME","PASSWORD");
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
mysqli_select_db($con, "rbxdataa_Data");
$Amount=$_GET["Amount"];
$GetType=$_GET["Type"];
$sql = "SELECT * FROM EventRecord WHERE EventType='$GetType' ORDER BY EventId DESC";
$sql_run = mysqli_query($con, $sql);
while($sql_row = mysqli_fetch_assoc($sql_run)){
echo $sql_row['EventId'].'<br>';
}
mysqli_close($con);
?>
As I am currently very new to PHP and MySQL, I have no idea why this code will not work. I am also confused as to how I would make it echo only the top ($Amount) as determined by how large the "EventId" value is.
My intent is to gather the [$Amount] highest rows in the table with the EventType $GetType.
I am aware of SQL Injection vulnerability, however for my purposes this does not affect me.
Error:
"Parse error: syntax error, unexpected T_STRING on line 4"
This isn't the main error, however. I just can't read the main error yet.
In your line 4, mysqli_connect_error() doesn't exist. Please check mysqli documentation http://www.php.net/manual/en/book.mysqli.php
Edit: You may try: printf("Connect failed: %s\n", mysqli_error($con));
Please use this code:
<?php
$con=mysqli_connect("HOST","USERNAME","PASSWORD");
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
mysqli_select_db($con, "rbxdataa_Data");
$Amount=$_GET["Amount"];
$GetType=$_GET["Type"];
$sql = "SELECT * FROM EventRecord WHERE EventType='$GetType' ORDER BY EventId DESC";
$sql_run = mysqli_query($con, $sql);
while($sql_row = mysqli_fetch_assoc($sql_run)){
echo $sql_row['EventId'].'<br>';
}
mysqli_close($con);
?>
Try this.
$con=mysqli_connect("HOST","USERNAME","PASSWORD","rbxdataa_Data");
$sql_query = "SELECT * FROM EventRecord WHERE EventType='$GetType' ORDER BY EventId DESC";
$sql_result = mysqli_query($con,$sql_query)
or exit("Query fail message");
Otherwise looks like what I'm used to.
I just realized that no one has even asked about the $_GET variable you're using you're using. You set $GetType from $_GET['Type']. It could be that.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
I am trying to get the count of rows returned with this code:
$selectLogin = "SELECT * FROM user WHERE username = "user" and access >= 1";
$queryLogin = sqlsrv_query($conn, $selectLogin);
$countLogin = sqlsrv_num_rows($conn, $queryLogin);
echo $countLogin;
And I am getting this error when calling the script:
PHP Fatal error: Param count and argument count don't match.
Any thoughts? I have tried the GOOGLE but it is not returning anything with solutions.
Try this way
$selectLogin = "SELECT * FROM user WHERE username = 'user' and access >= 1";
If you have $user variable already available.. Try like this
$selectLogin = "SELECT * FROM user WHERE username = '$user' and access >= 1";
You can try this,
$selectLogin = "SELECT * FROM user WHERE username = ? and access >= ? ";
$params = array('user', '1');
$queryLogin = sqlsrv_query($conn, $selectLogin, $params);
$countLogin = sqlsrv_num_rows($conn, $queryLogin);
echo $countLogin;