-2

So i'm trying to have it check to see if the steamid64 all ready exists before inserting but it just inserts any way? i'm not good with PHP here.

<?php
$con=mysqli_connect("localhost","user","pass","Gmod");

if (mysqli_connect_errno()) {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

$SteamID64 = mysqli_real_escape_string($con, $_POST['SteamID64']);
$enable = mysqli_real_escape_string($con, $_POST['enable']);

$sql="SELECT * FROM Loading (SteamID64, enable) WHERE SteamID64='$SteamID64'";
 if(mysql_num_rows($sql)>=1) {
echo'<center>The music is already disabled!</center>';
}
else {
$sql="INSERT INTO Loading (SteamID64, enable)
VALUES ('$SteamID64', '$enable')";
}

if (!mysqli_query($con,$sql)) {
  die('Error: ' . mysqli_error($con));
}
echo "<center>The music will not play when you connect.</center>";

mysqli_close($con);
?>

i got this off some forums and w3schools.com and edited it.

Ok so i did this but its still just inserting the data?

<?php
mysqli_report(MYSQLI_REPORT_STRICT);
$con=mysqli_connect("localhost","root","server","Gmod");
if (mysqli_connect_errno()) {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

$SteamID64 = mysqli_real_escape_string($con, $_POST['SteamID64']);
$enable = mysqli_real_escape_string($con, $_POST['enable']);

mysqli_query($con,"SELECT * FROM Loading WHERE SteamID64='$SteamID64'");
 if(mysqli_num_rows(mysqli_query)>=1) {
echo'<center>The music is already disabled!</center>';
}
else {
mysqli_query($con,"INSERT INTO Loading (SteamID64, enable)
VALUES ('$SteamID64', '$enable')");
}
echo "<center>The music will not play when you connect.</center>";

mysqli_close($con);
?>
57_Wolve
  • 5
  • 5

1 Answers1

1

Apart from the fact that you cannot mix mysql_* and mysqli_* functions like that (pick one, mysqli_* and stick to that), you have an error in your sql:

SELECT * FROM Loading (SteamID64, enable) WHERE SteamID64='$SteamID64'
                      ^^^^^^^^^^^^^^^^^^^ this should not be here

You need to change that to:

SELECT * FROM Loading WHERE SteamID64='$SteamID64'

You can have mysqli throw exceptions so that it tells you exactly what went wrong when it goes wrong. Just add this to the top of your script:

mysqli_report(MYSQLI_REPORT_STRICT);

Another problem you have, is that the execution of your second query should be inside the else part of the previous condition.

And as @FabrícioMatté already mentioned, you actually need to execute your query using mysqli_query(), just setting the string does not do anything.

jeroen
  • 91,079
  • 21
  • 114
  • 132
  • The code in the question is also missing a `mysqli_query`, and `mysql_num_rows` should be `mysqli`. – Fabrício Matté Aug 30 '14 at 00:16
  • @FabrícioMatté Thanks, I added your first point, the second one was already mentioned in the comments and in the first line of my answer. – jeroen Aug 30 '14 at 00:19
  • Oh ok, you can be more clear saying "You have to execute the query before calling `mysqli_num_rows`" and a 2 lines code block could help illustrating. – Fabrício Matté Aug 30 '14 at 00:35