PHP语法错误排查:执行$sqlselect赋值$id时遇T_VARIABLE错误
Hey there, let's dig into that syntax error you're hitting—those "unexpected variable" errors are usually tiny oversights that are easy to miss once you've been staring at code for a while!
What's Causing the Error?
That Parse error: syntax error, unexpected '$id' (T_VARIABLE) message means PHP got confused right before it hit $id. Almost always, this is because:
- You missed a semicolon at the end of the line right before
$id - A string (like your
$sqlselectquery) wasn't properly closed with quotes - You have mismatched curly braces or parentheses earlier in the code
Let's Fix Your Code
Looking at your snippet, the most likely culprit is a missing semicolon on your $delpassword assignment, or a broken string in your $sqlselect statement. Here's a corrected, safe version of your code (plus I added protection against SQL injection—super important when dealing with user input!):
<?php if (isset($_POST['delete'])) { // Grab form values (make sure these match your form's name attributes!) $delwebsite = $_POST['delWebsite']; $delusername = $_POST['delUsername']; $delpassword = $_POST['delPassword']; // Don't forget this semicolon! // Use prepared statements to avoid SQL injection $sqlselect = "SELECT id FROM your_table_name WHERE website = ? AND username = ? AND password = ?"; $stmt = $pdo->prepare($sqlselect); $stmt->execute([$delwebsite, $delusername, $delpassword]); $row = $stmt->fetch(PDO::FETCH_ASSOC); if ($row) { $id = $row['id']; // This line should now work without syntax errors // Now delete the record using the retrieved ID $deleteSql = "DELETE FROM your_table_name WHERE id = ?"; $deleteStmt = $pdo->prepare($deleteSql); $deleteStmt->execute([$id]); echo "Record deleted successfully!"; } else { echo "No matching record found with those details."; } } ?>
Quick Checks to Avoid This in the Future
- Double-check semicolons: Every variable assignment and standalone line needs to end with
;—PHP will throw a fit if you skip one. - Validate string quotes: If you're writing a SQL query in quotes, make sure you close them properly (e.g.,
"SELECT ..."can't end halfway through). - Ditch direct SQL concatenation: Never stick user input directly into your SQL strings—prepared statements like the example above are the only safe way to handle this.
内容的提问来源于stack exchange,提问作者zwerg4
相关产品推荐
相关产品推荐

