Comradeo Comradeo - 6 months ago 37
PHP Question

Fatal Error: PDO Statement Undefined Method & Undefined index?

I have tried over 32 uploaded solutions, and nothing seems to be right -- Can anyone assist me? I am getting undefined indexes, and when I solve those issues, I keep getting the fatal error listed below. No idea what I am doing wrong. ?_? Can someone enlighten me?

P.S; It's not a duplicate, fatal error included, not apart of the duplicate post, nor any relevancy.

Errors

Notice: Undefined index: somename in /homepages/29/d599905820/htdocs/index.php on line 22

Notice: Undefined index: username in /homepages/29/d599905820/htdocs/index.php on line 23

Fatal error: Call to undefined method PDOStatement::bind_param() in /homepages/29/d599905820/htdocs/index.php on line 26


HTML

<form action="" method = "POST">
<input type="radio" name="someName" value="ip"> IP<br>
<input type="radio" name="someName" value="uid"> UID<br>
Enter Name: <input type="text" name="name" > <br>
<input type="submit" name="submit">
</form>


PHP

<?php
error_reporting(E_ALL);
$submit=isset($_POST['submit']);
if($submit)
{
$name=$_POST['somename'];
$username=$_POST['username'];
$con = new PDO('mysql:host='.$db_hostname.';dbname='.$db_database.';charset=utf8mb4', $db_termname,$db_password, array(PDO::ATTR_EMULATE_PREPARES => false,PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION));
$stm=$con->prepare('SELECT * FROM users WHERE username = ?');
$stm->bind_param("s",$username);
$stmt->execute();
$row = $stm->fetch(PDO::FETCH_ASSOC);
if($name=='ip')
{ echo $row['ip'];
}
else if($name=='uid')
{
echo $row['uid];
}
}

Answer

You are mixing PDO with mysql and there are multiple syntax errors. Your code should be like this:

<?php
error_reporting(E_ALL);
$submit = $_POST['submit'];
if($submit) {
   $name = $_POST['somename'];
   $username = $_POST['username'];
   $con = new PDO('mysql:host='.$db_hostname.';dbname='.$db_database.';charset=utf8mb4', $db_termname,$db_password, array(PDO::ATTR_EMULATE_PREPARES => false,PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION));
   $stmt = $con->prepare('SELECT * FROM users WHERE username=:username');
   $stmt->bindParam(":username", $username);
   $stmt->execute();
   $row = $stmt->fetch(PDO::FETCH_ASSOC);
   if($name=='ip') {                       
      echo $row['ip'];  
   }elseif
      ($name=='uid') {
         echo $row['uid];
      }
}
?>

<form action="" method="POST">
  <input type="radio" name="somename" value="ip">IP<br>
  <input type="radio" name="somename" value="uid">UID<br>
  Enter Name: <input type="text" name="username">
  <br>
  <input type="submit" name="submit">
</form>

If I don't have any records for 'UID' or 'IP' how can I make a case in PHP that echoes 'No records found?

if($name=='ip') {  
   if($row['ip'] == 'ip') {
      echo 'Result!';
   }else{ 
      echo 'No Result!';
   }
}

if($name=='uid') {  
   if($row['uid'] == 'uid') {
      echo 'Result!';
   }else{ 
      echo 'No Result!';
   }
}