I'm trying to create an upvote system where, after checking to see if you're logged in and after getting your userid, it checks if the table has your userid with the postid already in it and if it does then it means it was already upvoted. I just want to know what's wrong in my code, this is being used for learning, I don't need any complex thing.
Code:
if (isset($_GET['upvote'])) {
if ($_SESSION["loggedin"] == true) {
$upvoteid = $_GET["upvote"];
$servername = "";
$username = "";
$password = "";
$dbname = "";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT id FROM users WHERE username=".$_SESSION["loggedinusername"];
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
$userid = $row["id"];
}
$sql2 = "SELECT userid, postid FROM upvotedposts WHERE userid='".$userid."' AND postid='".$upvoteid."'";
$result2 = $conn->query($sql);
if (!$result2->numrows > 0) {
$sql = "UPDATE posts SET upvotes = upvotes + 1 WHERE id = ".$upvoteid;
if ($conn->query($sql) === TRUE) {
echo "Sucessfully upvoted";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
}
} else {
echo "Failed;
}
$conn->close();
}
}
When I do click the upvote button, it simply does nothing. The issue here is that as far as I know it looks like it would work but I may be forgetting something that I am unaware of or incorrectly using something.
You are missing a closing "
echo "Failed;
should be:
echo "Failed";
Related
I'm trying to achieve something with php. I have two address on my admin table I'll like to display to my users. I want to display one of the address (id equals 3) to some specific user ids and the other address (id equals 4) to every other users id. Check my code below
<?php
$servername = "localhost";
$username = "user";
$password = "password";
$dbname = "user";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$id = "SELECT * FROM users WHERE id = '".$_SESSION['user']."'";
if ( ($id != 11303) && ($id != 27)) {
$sql = "SELECT address FROM admin WHERE id='3'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "" . $row["address"]. "";
}
} else {
echo "0 results";
}
}
else {
$sql = "SELECT address FROM admin WHERE id='4'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "" . $row["address"]. "";
}
} else {
echo "0 results";
}
}
$conn->close();
?>
I tried getting user id from their session then i used "if" to check if their session id equals to some specific ids then select the address with id 3 from the admin table and display it "else" it displays the address with id 4. But it's displaying the address with id 3 to everyone. Please can anyone help out?
Before use $_SESSION write start_session() at the top of your code.
then try:
$sql = mysqli_query($conn,"SELECT * FROM users WHERE id = '".$_SESSION['user']."'");
$result = mysqli_fetch_assoc($sql);
$id = $result['id'];
Now, Put $id value in your condition
if ( ($id != 11303) && ($id != 27))
{
//do something
}
This is delete.php
<?php
if(isset($_GET['id']) && !empty($_GET['id']))
{
$id = $_GET['id'];
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "test";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "DELETE FROM table WHERE memberid IN ('".$id."' )";
if ($conn->query($sql) === TRUE) {
echo "Record updated successfully" ;
} else {
echo "Error updating record: " . $conn->error;
}
$conn->close();
}
?>
this is home.php
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "select memberid from table group by memberid having count(*) > 5";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "Member ID: " . $row["memberid"]. " | <a href='javascript:void(0);' onClick='deleteId(".$row["memberid"].")'>reset</a><br>";
}
} else {
echo "0 results";
}
$conn->close();
<script>
function deleteId(id)
{
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200)
{
alert(xmlhttp.responseText);
}
};
xmlhttp.open("GET", "delete.php?id=" +id, true);
xmlhttp.send();
//window.location.reload();
}
</script>
I'm trying to delete multiple row in a table by onclick from home.php unfortunately it doesnt work at all.
in the table row recorded many same memberid, so i'm trying to call by memberid and delete all of them in 1click
I'm new in php and mysql.
Thanks for the help.
$sql = "DELETE FROM table WHERE c ='$id'";
Try this:
$idArr = explode(",", $id);
$idStr = implode("','", $idArr);
$sql = "DELETE FROM table WHERE c IN ('$idStr' )";
Also, in your client side code,
Make sure that the id in deleteId() is alerting 012345 instead of 12345. Otherwise, convert the integer to string before calling deleteId().
You are sending in one id, so no need for using IN(), normal id = ? will delete all rows with that id. But you need to use bind parameters to stop someone from sending in something like 1 or 1=1 to delete all the rows in your table, don't trust the input even if it is sent from your javascript
Phpmyadmin:
Phpmyadmin Image example
if (mysql_query("SELECT setup FROM users") === 1) {
echo "One";
} else if (mysql_query("SELECT setup FROM users") === 0) {
echo "Zero";
}
On register. As defined: 0.
If table shows 0, echo Zero.
Else if table shows ID 1, echo One.
How is this done?
Solution:
$setup = mysql_query("SELECT setup FROM users");
$row = mysql_fetch_assoc($setup);
if ($row['setup'] == 0) {
echo "Zero.";
} else {
echo "One!";
}
I think you need this
First of all use mysqli
//$connection = your mysqli connection. Dont use mysql
Then properly write query if you need admin row
$query = "SELECT `setup` FROM `users` WHERE `user_id`=1 AND `username` = 'admin'";
Then execute query
$result = mysqli_query($connection, $query);
Then search for $setup value
while($row = mysqli_fetch_assoc($result)){
$setup = $row['setup'];
}
Then echo what you need
if ($setup == 1) {
echo "One";
} else if ($setup == 0) {
echo "Zero";
}
See the docs. The 'Example (MySQLi Procedural)' example is similar to what you want.
Snippet:
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$sql = "SELECT id, firstname, lastname FROM MyGuests";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
echo "id: " . $row["id"]. " - Name: " . $row["firstname"]. " " . $row["lastname"]. "<br>";
}
} else {
echo "0 results";
}
mysqli_close($conn);
?>
Changing
$sql = "SELECT id, firstname, lastname FROM MyGuests";
to
$sql = "SELECT setup FROM users";
then
if (mysqli_num_rows($result) > 0) {
to your various if checks should suffice. Not the cleanest. Seems like you could just print out the value of setup? Good luck!
i have been trying to get this script done for a while now - im kind of new to php and mysql but i have been trying to get this to check the db for the username and then if the username exists - stop checking the db and if it doesn't exists add it to the db.
here is my code:
//input from application
$test = "wheelsmanx";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT mainusername FROM CCCpro_test";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
if ($row["mainusername"] === $test) {
echo "User Name Already In Use.";
}if($row["mainusername"] !== $test){
echo "this statement";
[code that inserts into db i can do this part myself]
}
}
$conn->close();
} else {
echo "0 results";
}
$conn->close();
The problem with your code is that you do the INSERT of the new name inside an if statement that has confirmed the existence of that user already. In addition I think you messed up your SELECT statement by selecting all the users.
Look into INSERT ON DUPLICATE for a better way to do it, or revise your code as below.
$sql = "SELECT mainusername FROM CCCpro_test WHERE mainusername = $test";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
echo "User Name Already In Use.";
}
else{ //no rows selected therefore the user doesn't exist
[code that inserts into db i can do this part myself]
}
$conn->close();
PLEASE READ I have somewhere to go so I am being lazy so I did not bind the $test variable therefore DO NOT copy and paste this code without updating it to bind the $test variable. Please read this post about PDO and variable binding to prevent SQL injection.
here is my full working code if anyone needs it - it uses the post method - from an html form .... in case some one needs to hack it to pieces for something else
well guys i appreciate all of your help :D but i have found an answer or a way around it i suppose- i thought of it all night and day on how i could make it work and i came up with this
$servername = "127.0.0.1";
$username = "TESTUSER";
$password = "TESTPASS";
$dbname = "TESTDB";
$testusername = $_POST['mainusername'];
$testpassword = $_POST['mainpassword'];
//input from application
$test = $_POST['mainusername'];
$test2 = "0";
//Count switch
$countswitch = "0";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql1 = "INSERT INTO CCCpro_test ( mainusername, mainpassword ) VALUES ('$testusername','$testpassword' )";
$sql = "SELECT mainusername FROM CCCpro_test";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
if ($row["mainusername"] === $test) {
echo "Im Sorry Username Already In Use";
$countswitch ++;
}
}
if($countswitch == $test2){
echo "User Name Registered";
$db_handle = mysql_connect($servername, $username, $password);
$db_found = mysql_select_db($dbname, $db_handle);
if ($db_found) {
$result1 = mysql_query($sql1);
mysql_close($db_handle);
}
}
if ($countswitch == 3){
echo "this";
}
} else {
echo "0 results";
}
$conn->close();
I'm working on a web form that allows the user to conduct a zip code search. My intent is to use the form post to search a MySQL table for the matching zip code then return content from an associated field in the same row.
I can get as far as... form submits and directs user to the intended form_post.php page. If I enter <?php echo $_POST["zipcode"]; ?> on the page it returns the content submitted by the user.
I followed the PHP/MySQLi examples from these pages:
http://www.w3schools.com
/php/php_mysql_connect.asp
/php/php_mysql_select.asp
/php/php_if_else.asp
I am able to connect to the database and return the 2 selected values from the table. This is my example so far:
<?php
$servername = "localhost";
$username = "db_username";
$password = "db_password";
$dbname = "db_name";
if (isset($_POST['zipcode'])) {
$zipcode = $_POST['zipcode'];
}
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT zip_code, area FROM zipcode_table WHERE zip_code = $zipcode";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "Result: " . $row["zip_code"]. " " . $row["area"]. "<br>";
}
} else {
echo "0 results";
}
$conn->close();
?>
After the query returns the associated values I would like to determine if the returned value from 'area' is one of 3 values then forward the user to the appropriate url.
I was working on an if/else statement as follows then got stuck.
if($result == "A") {
header("Location: http://example.com/page-1/");
}
elseif($result == "B") {
header("Location: http://example.com/page-2/");
}
elseif($result == "C") {
header("Location: http://example.com/page-3/");
} else {
header("Location: http://example.com/");
exit();
}
Any recommendations are appreciated.
you are using $result in if but, you not assign it area region.So, first relpace this:
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "Result: " . $row["zip_code"]. " " . $row["area"]. "<br>";
}
} else {
echo "0 results";
}
with this:
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "Result: " . $row["zip_code"]. " " . $row["area"]. "<br>";
$result = $row["area"];
break;
}
} else {
echo "0 results";
$result = "";
}
then replace query:
$sql = "SELECT zip_code, area FROM zipcode_table WHERE zip_code = $zipcode";
with this:
$sql = "SELECT zip_code, area FROM zipcode_table WHERE zip_code = '$zipcode'";
then your problem will be solved.