2

Okay so I have this really simple form that looks up values in my db tables. I haven't fully completed it yet and I was chugging along okay until I wanted to use a Jquery datepicker(I want to be able to easily select within a large custom set of years).

Due to my own reasons, the tables on my db don't use total date values so I split up the value into strings to lookup (worked fine with html5, but its not seeming to work with jquery)

Well, now I get "Call to a member function data_seek() on a non-object in" on my code. I've been tearing it apart and trying to fix things for several hours now and I kindly seek your help SO community:

Here is the PHP code:

   <?php
 $estring = substr($_POST[idvalue],-10, 5);
 $wstring = substr($_POST[idvalue],6);
 $mysqli = mysqli_connect("localhost", "usr", "", "xyz");
     echo $estring."<br>";
     echo $wstring;
$stmt = $mysqli->prepare('SELECT * FROM western WHERE id = ?');
$stmt->bind_param('s', $wstring);
$stmt->execute();
while ($row = $stmt->fetch()) {
    echo  "sign: ". $row['Western Zodiac Description']."<br>";
    echo " Birth Card: = " . $row['Birth Card'];
    echo " : =" . $row['Birth Card Descripton']."<br>";
    echo "Planetary Card =". $row['Planatray Card'];
    echo " : ". $row['Planatary Card Description']."<br>";
    echo " 2nd Planetary Card =". $row['2nd Planatray Card'];
    echo ": " . $row['2nd Planatary Card Description']."<br>";
}

$mysqli->close();      
?>

Here is the frontend HTML:

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>project </title>
  <link rel="stylesheet" href="//code.jquery.com/ui/1.11.2/themes/smoothness/jquery-ui.css">
  <script src="//code.jquery.com/jquery-1.10.2.js"></script>
  <script src="//code.jquery.com/ui/1.11.2/jquery-ui.js"></script>
  <link rel="stylesheet" href="/resources/demos/style.css">
  <script>
  $(function() {
    $( "#datepicker" ).datepicker({
      yearRange: "1645:2112",
      changeMonth: true,
      changeYear: true
      });
  });
  </script>
</head>

<body>
<br>

<form action="outputb.php" method="post">
 enter date: <input type="text" id="datepicker" name='idvalue'>
 <br>
 <br>
  <input type="submit" value="Search"/>
</form>

</body>
</html>
Frankenmint
  • 1,570
  • 3
  • 18
  • 33
  • `WHERE id = '$wstring'`. The is no `IS` keyword in MySQL for equality. – Michael Berkowski Oct 26 '14 at 15:53
  • Now is the time to begin learning to use [`prepare()/execute()`](http://php.net/manual/en/mysqli.prepare.php) in MySQLi to bind your parameter `$wstring` correctly with a `?` placeholder. See http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php to protect this script against SQL injection, to which it is currently vulnerable. – Michael Berkowski Oct 26 '14 at 15:54
  • Sorry, I tried both, that didn't fix it and forgot change it back – Frankenmint Oct 26 '14 at 15:54
  • You always need to check for errors. If `mysqli_query()` failed, you need to verify with `if (!$res) echo mysqli_error($mysqli);` to get error details. – Michael Berkowski Oct 26 '14 at 15:55
  • Other problem - you are using a single-quote string around the SQL statement. That means the variable `$wstring` is _not_ being expanded in the string and you are literally sending in the string `'$wstring'` rather than variable contents. Read about the differences between single & double quoting here: http://php.net/manual/en/language.types.string.php – Michael Berkowski Oct 26 '14 at 15:56

1 Answers1

2

First change mysqli connection to

$mysqli = new mysqli("localhost", "usrnm", "xxxyz", "db");
if ($mysqli->connect_errno) {
    die( $mysqli->connect_error);
}

Then change query string

$query = "SELECT * FROM western WHERE id IS '$wstring'";

To

$query = "SELECT * FROM western WHERE id = '$wstring'";

Also add error reporting ($mysqli->error) and final suggestion:

if ($res = $mysqli->query($query)){
    //$res->data_seek(0);
    if ($res->num_rows == 0){ // use if required
        echo 'no data found!';
    }
    while ($row = $res->fetch_assoc()){
        echo "sign: ". $row['Western Zodiac Description']."<br>";
        echo " Birth Card: = " . $row['Birth Card'];
        echo " : =" . $row['Birth Card Descripton']."<br>";
        echo "Planetary Card =". $row['Planatray Card'];
        echo " : ". $row['Planatary Card Description']."<br>";
        echo " 2nd Planetary Card =". $row['2nd Planatray Card'];
        echo ": " . $row['2nd Planatary Card Description']."<br>";
    }

    echo 'DEBUG OUTPUT <br>';
    echo $estring."<br>";
    echo $wstring;

    $mysqli->close();
}else{
    die($mysqli->error);
}
MH2K9
  • 11,951
  • 7
  • 32
  • 49
  • That error will not trigger if my data returns zero results (I don't know if its because '0 results' counts as non-null) – Frankenmint Oct 26 '14 at 16:21
  • 1
    Returning zero results is not technically an error state. If you need to find that out, use `if ($res->num_rows > 0)` Note also that there is no need to call `data_seek(0)` here, since you have not advanced the recordset pointer in `$res` at that time. You can just omit that line entirely and proceed from `query()` to `fetch_assoc()` (after testing for errors from `query() and checking `num_rows` if desired) – Michael Berkowski Oct 26 '14 at 18:03
  • This did not help me resolve 'why it does not work when using jquery' I'm resorting to html5 as I can seem to get a reliable response. I marked yours as best answer because you went above and beyond to try helping me out (rewording the code several times) thank you. – Frankenmint Oct 26 '14 at 20:20