Click here to Skip to main content
15,923,083 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Can anyone tell me why php 7.0.8 shows error while running the following php code and can anyone change the following php code in mysqli?
PHP
<?php

$host="localhost"; // Host name
$username=""; // Mysql username
$password=""; // Mysql password
$db_name="test"; // Database name
$tbl_name="forum_answer"; // Table name

// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");

// Get value of id that sent from hidden field
$id=$_POST['id'];

// Find highest answer number.
$sql="SELECT MAX(a_id) AS Maxa_id FROM $tbl_name WHERE question_id='$id'";
$result=mysql_query($sql);
$rows=mysql_fetch_array($result);

// add + 1 to highest answer number and keep it in variable name "$Max_id". if there no answer yet set it = 1
if ($rows) {
$Max_id = $rows['Maxa_id']+1;
}
else {
$Max_id = 1;
}

// get values that sent from form
$a_name=$_POST['a_name'];
$a_email=$_POST['a_email'];
$a_answer=$_POST['a_answer'];

$datetime=date("d/m/y H:i:s"); // create date and time


// Insert answer
$sql2="INSERT INTO $tbl_name(question_id, a_id, a_name, a_email, a_answer, a_datetime)VALUES('$id', '$Max_id', '$a_name', '$a_email', '$a_answer', '$datetime')";
$result2=mysql_query($sql2);

if($result2){
echo "Successful<BR>";
echo "<a href='view_topic.php?id=".$id."'>View your answer</a>";


// If added new answer, add value +1 in reply column
$tbl_name2="forum_question";
$sql3="UPDATE $tbl_name2 SET reply='$Max_id' WHERE id='$id'";
$result3=mysql_query($sql3);

}
else {
echo "ERROR";
}

// Close connection

mysql_close();
?>


What I have tried:

I tried to run this php code in php 7.0.8 Version but it shows the error in it. So, now I am trying to convert this code into the mysqli, can anyone help me ?
Posted
Updated 20-Oct-16 2:57am
v2
Comments
Suvendu Shekhar Giri 21-Oct-16 17:39pm    
What is the error it showing?
S.Shrestha 23-Oct-16 11:10am    
These are the errors it showing:

Notice: Undefined index: id in C:\xampp\htdocs\simple_forum\add_answer.php on line 22

Notice: Undefined index: a_name in C:\xampp\htdocs\simple_forum\add_answer.php on line 41

Notice: Undefined index: a_email in C:\xampp\htdocs\simple_forum\add_answer.php on line 42

Notice: Undefined index: a_answer in C:\xampp\htdocs\simple_forum\add_answer.php on line 43

Fatal error: Uncaught Error: Call to undefined function mysql_query() in C:\xampp\htdocs\simple_forum\add_answer.php:50 Stack trace: #0 {main} thrown in C:\xampp\htdocs\simple_forum\add_answer.php on line 50

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900