-1

So I have a page using PHP and a MySQL query. What I'm wanting to do is create basically an "edit" page that takes data from my database and uses it to show the values in various inputs. The user can then change the data in the input which will then update the corresponding MySQL table row. However, for whatever reason the page is NOT displaying the form, but rolling over to the else statement. I can verify the $_SESSION['weaponName'] is working, because it will echo the correct thing. Any ideas on why the form will not show up for me?

edit.php

   <?php
session_start();

$con=mysqli_connect("localhost","username","password","db_name");
// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

$weaponName = $_SESSION['weaponName'];

$query = mysqli_query($con, "SELECT * FROM weapons limit 1");

if(mysqli_num_rows($query)>=1){
while($row = mysqli_fetch_array($query)) {
    $creator= $row['creator'];
    $weaponCategory= $row['weaponCategory'];
    $weaponSubCategory= $row['weaponSubCategory'];
    $costAmount= $row['costAmount'];
    $costType= $row['costType'];
    $damageS= $row['damageS'];
    $damageM= $row['damageM'];
    $critical= $row['critical'];
    $rangeIncrement= $row['rangeIncrement'];
    $weight= $row['weight'];
    $weaponType= $row['weaponType'];
    $masterwork= $row['masterwork'];
    $attributes= $row['attributes'];
    $specialAbilities= $row['specialAbilities'];
    $additionalInfo= $row['additionalInfo'];
}

?>

<form action="weaponEditUpdate.php" method="post">
    <input type="hidden" name="weaponName" value="<?php echo $weaponName;?>">

    Weapon Name: <input type="text" name="weaponName" value="<?php echo $weaponName;?>">
    <br>
    Weapon Category: <select name="weaponCategory">
    <?php while ($row = mysqli_fetch_array($query)) {
    echo "<option value='" . $row['weaponCategory'] ."'>" . $row['weaponCategory'] ."</option>";
    } ?>
    </select>
    <input type="Submit" value="Change">
</form>

<?php
}else{
    echo 'No entry found. <a href="javascript:history.back()">Go back</a>';
}
?>
Community
  • 1
  • 1
doriansm
  • 247
  • 1
  • 5
  • 31
  • 4
    Right off the bat, you're mixing `mysqli_` with `mysql_` so that's a no go. You're connecting with `mysqli_` but querying with `mysql_` – Funk Forty Niner Feb 14 '14 at 22:34
  • What's wrong with an array? Just change your `if` to `if ($row = mysql_fetch_array($query))` and remove the while loop. You have all the information you need in the `$row` variable. – jeroen Feb 14 '14 at 22:39
  • (FYI)=> There are just too many things wrong with this code, which is why I didn't bother putting in an answer. – Funk Forty Niner Feb 14 '14 at 22:40
  • Hey my bad i completely forgot I was connecting with mysqli. I know, retarded. Anyways I have taken all your advice but still no luck. Also, if I use simply just the `if` statement as mentioned by @jeroen the page is just blank. Please excuse my ignorance. In case any one had any other thoughts on this I have updated the code to show changes made. – doriansm Feb 14 '14 at 22:54
  • I don't see any changes here. Edit: ah, here we go, it just kicked in. – Funk Forty Niner Feb 14 '14 at 22:55
  • You'd still need to change to the mysqli variant of the function (I just copied your code to illustrate)... – jeroen Feb 14 '14 at 22:55
  • @Fred-ii- changes made. – doriansm Feb 14 '14 at 22:56
  • Instead of `if(mysqli_num_rows($query)>=1){` can you try `if(mysqli_num_rows($query) >0){` @Aldentec or `if(mysqli_num_rows($query) ==1){` – Funk Forty Niner Feb 14 '14 at 22:57
  • `if(mysqli_num_rows($query) >0){` did the trick. Thank you so much @fred -ii- – doriansm Feb 14 '14 at 22:58
  • Can you post that as an answer so I can mark it? – doriansm Feb 14 '14 at 22:59
  • Plus, you may also need to add `$weaponName = $_POST['weaponName'];` @Aldentec – Funk Forty Niner Feb 14 '14 at 23:00
  • Just did, glad it got resolved :) @Aldentec and you're welcome. – Funk Forty Niner Feb 14 '14 at 23:01

4 Answers4

1

change this

 $query = mysql_query("SELECT * FROM weapons limit 1");

to

 $query = mysqli_query("SELECT * FROM weapons limit 1");

BUT omg all your code is mysql while you connected by mysqli !! .

echo_Me
  • 37,078
  • 5
  • 58
  • 78
1

You're mixing functions

mysqli_connect("localhost","username","password","db_name");

Won't work with

mysql_query("SELECT * FROM weapons limit 1");

Try

$query = mysqli_query($con, "SELECT * FROM weapons limit 1");

And then

if($query->num_rows >= 1)
Machavity
  • 30,841
  • 27
  • 92
  • 100
1

As requested by OP (from comment conversations)

Instead of

if(mysqli_num_rows($query)>=1){ 

use

if(mysqli_num_rows($query) >0){
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
0

You connect with mysqli, which is fine. THEN, you attempt to run queries via mysql. Those are two separate extensions. You can't mix them as they won't "communicate" with one another. Stick to mysqli.

Wayne Whitty
  • 19,513
  • 7
  • 44
  • 66