$new_topic_id = mysqli_insert_id($conn);
I saw a video where the person just wrote the following without including any database:
$new_topic_id = mysql_insert_id();
but when I did it , i get an error, expecting 1 parameter instead of 0…
$new_topic_id = mysqli_insert_id($conn);
I saw a video where the person just wrote the following without including any database:
$new_topic_id = mysql_insert_id();
but when I did it , i get an error, expecting 1 parameter instead of 0…
use the MySQL LAST_INSERT_ID function to get the last insert id that MySQL has been generated.
Example : INSERT INTO tbl(description)
VALUES('record 1'),
('record 2'),
('record 3');
SELECT LAST_INSERT_ID(); //
Suppose the AUTO_INCREMENT column has current value as 1 and you insert 3 rows into the table.
When you use the LAST_INSERT_ID function to get the last insert id, you will get 2 instead of 4.
Sorry for the confusion here but I should include my whole code and the video that I am following: I do get an undefined $topics variable… it was working before:
<?php
include_once 'header.php';
if (!isset($_SESSION['u_uid'])) {
header("Location: index.php?view_category=notlogin");
exit();
} else {
include_once 'includes/dbh.php';
$cid = $_GET['cid'];
$logged = " <a href='create_topic.php?cid=".$cid."'>Click here to create a topic</a>";
$sql = "SELECT id FROM categories WHERE id= '".$cid."' LIMIT 1";
$stmt = mysqli_stmt_init($conn);
if (!mysqli_stmt_prepare($stmt, $sql)) {
echo 'SQL error';
} else {
// mysqli_stmt_bind_param($stmt, "ii", $cid, $limit);
mysqli_stmt_execute($stmt);
$result = mysqli_stmt_get_result($stmt);
$resultCheck = mysqli_num_rows($result);
if ($resultCheck ==1) {
$sql2 = "SELECT * FROM topics WHERE category_id= '".$cid."' ORDER BY topic_reply_date DESC;";
$stmt = mysqli_stmt_init($conn);
if (!mysqli_stmt_prepare($stmt, $sql2)) {
echo 'SQL error';
} else {
//mysqli_stmt_bind_param($stmt, "i", $cid);
mysqli_stmt_execute($stmt);
$result2 = mysqli_stmt_get_result($stmt);
$resultCheck2 = mysqli_num_rows($result2);
if ($resultCheck2 > 0) {
$topics .= "<table width='100%' style='border-collapse: collapse:'>";
$topics .= "<tr><td colspan='3'><a href='forum.php'>Return to Forum Index</a>".$logged."</td></tr>";
$topics .= "<tr style='background-color: #dddddd:'><td>Topic Title</td>><td width='65' align='center'>Replies</td><td width='65' align='center'>Views</td></tr>";
$topics .= "<tr><td colspan='3'><hr></td></tr>";
while ($row = mysqli_fetch_assoc($result2)) {
$tid = $row['id'];
$title = $row['topic_title'];
$views = $row['topic_views'];
$date = $row['topic_date'];
$creator = $row['topic_creator'];
$topics .= "<tr><td><a href='view_topic.php?cid=".$cid."&tid=".$tid."'>".$title."</a><br /><span class='post_info'>Posted by: ".$creator." on ".$date."</span></td><td align='center'>0</td><td align='center'>".$views."</td></tr>";
$topics .= "<tr><td colspan='3'><hr /></td></tr>";
}
$topics .= "</table>";
} else {
echo "<a href='header.php'>Return to the Forum page</a>";
echo "<p> There are no topics in this Category yet.".$logged."</p>";
}
}
}else {
echo "<a href='header.php'>Return to the Forum page</a>";
echo "<p> You are trying to view a catebory that does not exists yet.</p>";
}
}
}
I have been tryng to follow this tutorial:
I know where is the problem… It is in this line:
$sql2 = “SELECT * FROM topics WHERE category_id= '”.$cid."’ ORDER BY topic_reply_date DESC;";
when I changed it to:
$sql2 = “SELECT * FROM topics WHERE category_id= 6 ORDER BY topic_reply_date DESC;”;
Did I concatenate that wrong?
Never mind…
I still have an error undefined $topics
I am just wondering and I guess that you can’t… Can you use mysql functions with mysqli? For example: Is the code below only use for mysql or mysqli? I am just not sure if the if statement makes sense?
$result = mysql_query.....
if (($result) && ($result2) && ($result3)) {
header("Location: view_topic.php?id=".$cid."&tid=".$new_topic_id);
} else {
echo 'There was a problem';
}
Sorry for the confusion again but I am trying to get this code to work with prepared statement and I got sql error:
<?php
include_once 'header.php';
if (!isset($_SESSION['u_uid'])) {
header("Location: index.php?view_category=notlogin");
exit();
} else {
include_once 'includes/dbh.php';
$cid = $_GET['cid'];
$logged = " <a href='create_topic.php?cid=".$cid."'>Click here to create a topic</a>";
}
$limit = 1;
$sql = "SELECT id FROM categories WHERE id= ? LIMIT =?";
$stmt = mysqli_stmt_init($conn);
if (!mysqli_stmt_prepare($stmt, $sql)) {
echo 'SQL error';
} else {
mysqli_stmt_bind_param($stmt, "ii", $cid, $limit);
mysqli_stmt_execute($stmt);
$result = mysqli_stmt_get_result($stmt);
$resultCheck = mysqli_num_rows($result);
if ($resultCheck ==1) {
$sql2 = "SELECT * FROM topics WHERE category_id= ? ORDER BY topic_reply_date DESC;";
$stmt = mysqli_stmt_init($conn);
if (!mysqli_stmt_prepare($stmt, $sql2)) {
echo 'SQL error';
} else {
mysqli_stmt_bind_param($stmt, "i", $cid);
mysqli_stmt_execute($stmt);
$result2 = mysqli_stmt_get_result($stmt);
$resultCheck2 = mysqli_num_rows($result2);
if ($resultCheck2 > 0) {
$topics .= "<table width='100%' style='border-collapse: collapse:'>";
$topics .= "<tr><td colspan='3'><a href='forum.php'>Return to Forum Index</a>".$logged."</td></tr>";
$topics .= "<tr style='background-color: #dddddd:'><td>Topic Title</td>><td width='65' align='center'>Replies</td><td width='65' align='center'>Views</td></tr>";
$topics .= "<tr><td colspan='3'><hr></td></tr>";
while ($row = mysqli_fetch_assoc($result2)) {
$tid = $row['id'];
$title = $row['topic_title'];
$views = $row['topic_views'];
$date = $row['topic_date'];
$creator = $row['topic_creator'];
$topics .= "<tr><td><a href='view_topic.php?cid=".$cid."&tid=".$tid."'>".$title."</a><br /><span class='post_info'>Posted by: ".$creator." on ".$date."</span></td><td align='center'>0</td><td align='center'>".$views."</td></tr>";
$topics .= "<tr><td colspan='3'><hr /></td></tr>";
}
$topics .= "</table>";
echo $topics;
} else {
echo "<a href='header.php'>Return to the Forum page</a>";
echo "<p> There are no topics in this Category yet.".$logged."</p>";
}
}
}else {
echo "<a href='header.php'>Return to the Forum page</a>";
echo "<p> You are trying to view a catebory that does not exists yet.</p>";
}
}
I got it to work now by doing the following query:
$sql = “SELECT id FROM categories WHERE id= ? LIMIT ?”;
I don’t think you need an = sign but I am now getting the following error:
Here is my code:
<?php
include_once 'header.php';
if (!isset($_SESSION['u_uid'])) {
header("Location: index.php?view_category=notlogin");
exit();
} else {
include_once 'includes/dbh.php';
$cid = $_GET['cid'];
$logged = " <a href='create_topic.php?cid=".$cid."'>Click here to create a topic</a>";
}
$limit = 1;
$sql = "SELECT id FROM categories WHERE id= ? LIMIT ?";
$stmt = mysqli_stmt_init($conn);
if (!mysqli_stmt_prepare($stmt, $sql)) {
echo 'SQL error';
} else {
mysqli_stmt_bind_param($stmt, "ii", $cid, $limit);
mysqli_stmt_execute($stmt);
$result = mysqli_stmt_get_result($stmt);
$resultCheck = mysqli_num_rows($result);
if ($resultCheck ==1) {
$sql2 = "SELECT * FROM topics WHERE category_id= ? ORDER BY topic_reply_date DESC;";
$stmt = mysqli_stmt_init($conn);
if (!mysqli_stmt_prepare($stmt, $sql2)) {
echo 'SQL error';
} else {
mysqli_stmt_bind_param($stmt, "i", $cid);
mysqli_stmt_execute($stmt);
$result2 = mysqli_stmt_get_result($stmt);
$resultCheck2 = mysqli_num_rows($result2);
if ($resultCheck2 > 0) {
$topics .= "<table width='100%' style='border-collapse: collapse:'>";
$topics .= "<tr><td colspan='3'><a href='forum.php'>Return to Forum Index</a>".$logged."</td></tr>";
$topics .= "<tr style='background-color: #dddddd:'><td>Topic Title</td>><td width='65' align='center'>Replies</td><td width='65' align='center'>Views</td></tr>";
$topics .= "<tr><td colspan='3'><hr></td></tr>";
while ($row = mysqli_fetch_assoc($result2)) {
$tid = $row['id'];
$title = $row['topic_title'];
$views = $row['topic_views'];
$date = $row['topic_date'];
$creator = $row['topic_creator'];
$topics .= "<tr><td><a href='view_topic.php?cid=".$cid."&tid=".$tid."'>".$title."</a><br /><span class='post_info'>Posted by: ".$creator." on ".$date."</span></td><td align='center'>0</td><td align='center'>".$views."</td></tr>";
$topics .= "<tr><td colspan='3'><hr /></td></tr>";
}
$topics .= "</table>";
echo $topics;
} else {
echo "<a href='header.php'>Return to the Forum page</a>";
echo "<p> There are no topics in this Category yet.".$logged."</p>";
}
}
}else {
echo "<a href='header.php'>Return to the Forum page</a>";
echo "<p> You are trying to view a catebory that does not exists yet.</p>";
}
}
Silly me… I look at all the youtube comments and I have to put $topics=""; at the top… It is working now!