mardi 5 mai 2015

MySQL result variable not functioning PhP [duplicate]

This question already has an answer here:

My $result variable is causing the error: ( ! ) Parse error: syntax error, unexpected '$result' (T_VARIABLE) in C:\wamp\www\testFunction.php on line 30

Ive search the other threads and I think its coded correctly

<?php
require_once 'login.php';
$conn = new mysqli($hn, $un, $pw, $db);
if ($conn->connect_error) die($conn->connect_error);

if (isset($_POST['delete']) && isset($_POST['ContractorID']))
{
  $ContractorID  = get_post($conn, 'ContractorID');
  $query  = "DELETE FROM Professionals WHERE 'ContractorID' ='$ContractorID'";
  $result = $conn->query($query);
  if (!$result) echo "DELETE failed: $query<br>" .
    $conn->error . "<br><br>";
}

if (isset($_POST['Name'])   &&
    isset($_POST['Title'])    &&
    isset($_POST['Company']) &&
    isset($_POST['Address']) &&
    isset($_POST['ContractorID']))
{
  $Name         = get_post($conn, 'Name');
  $title        = get_post($conn, 'Title');
  $Company      = get_post($conn, 'Company');
  $Address      = get_post($conn, 'Address');
  $ContractorID = get_post($conn, 'ContractorID');
        $query    = "INSERT INTO Professionals VALUES" .
                    "('$Name', '$title', '$Company', '$Address', '$un');"


  $result   = $conn->query($query);


  if (!$result) echo "INSERT failed: $query<br>" .
    $conn->error . "<br><br>";
}

echo <<<_END
<form action="AthleteData.php" method="post"><pre>
Name         <input type="text" name="Name">
Title        <input type="text" name="Title">
Company      <input type="text" name="Company">
Address      <input type="text" name="Address">
                  <input type="submit" value="ADD RECORD">
</pre></form>
_END;

$query  = "SELECT 'Name', 'Title', 'Company', 'Address' FROM Professionals WHERE Athlete = '$un'";
$result = $conn->query($query);
if (!$result) die ("Database access failed: " . $conn->error);

$rows = $result->num_rows;
//'<table>';
for ($j = 0 ; $j < $rows ; ++$j)
{
  $result->data_seek($j);
  $row = $result->fetch_array(MYSQLI_NUM);
//'<td>';
  echo <<<_END
<pre>
   Name $row[0]
   Title $row[1]
   Company $row[2]
   Address $row[3]
   ContractorID $row[4]
</pre>
<form action="AthleteData.php" method="post">
<input type="hidden" name="delete" value="yes">
<input type="hidden" name="ContractorID" value="$row[4]">
<input type="submit" value="DELETE RECORD"></form>
_END;
//'</td>';
}
//'</table>';

$result->close();
$conn->close();

function get_post($conn, $var)
{
  return $conn->real_escape_string($_POST[$var]);
}
?>

This is the snippet that is causing problems. I included all the code for reference if needed previously. Thank you for your help.

if (isset($_POST['Name'])   &&
    isset($_POST['Title'])    &&
    isset($_POST['Company']) &&
    isset($_POST['Address']) &&
    isset($_POST['ContractorID']))
{
  $Name         = get_post($conn, 'Name');
  $title        = get_post($conn, 'Title');
  $Company      = get_post($conn, 'Company');
  $Address      = get_post($conn, 'Address');
  $ContractorID = get_post($conn, 'ContractorID');
        $query    = "INSERT INTO Professionals VALUES" .
                    "('$Name', '$title', '$Company', '$Address', '$un');"


  $result   = $conn->query($query);


  if (!$result) echo "INSERT failed: $query<br>" .
    $conn->error . "<br><br>";
}

The compiler is saying that the $result is unexpected and I dont know why. The other posts say to write the query into the conn->query parameter. I did by writing the query into a variable.

Aucun commentaire:

Enregistrer un commentaire