MySQL Forums
Forum List  »  PHP

Re: Connecting myPHPadmin to web page
Posted by: Barry Galbraith
Date: October 03, 2017 10:23PM

Several things.
As PB says, mysql interface is deprecated and will go away. Use mysqli interface instead.
In your HTML, <form> has a default "action" of the file itself. You only need to specify action="" if you want the POST handled by a different file name.

This file will get you going.

<head><title>Insert People</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf8"> <!-- sends utf8 characters from your form --!>
<form method="post"> 
<label>* First Name<br><input type="text" name="fName" size="33" value="First" required></label><br> 
<label>* Last Name<br><input type="text" name="lName" size="33" value="Last" required></label><br>	

<input type="submit" value="add"> 


if($_POST){ //This will test if POST has been made. $_POST['submit'] doesn't exist.
$link = mysqli_connect("localhost",  "root", "root","test") or die ("not connected"); // mysqli_connect takes a database name

mysqli_set_charset ( $link , "utf8" ); //tells php what character set to expect for the mysqli_real_escape_string() below

$fName = $_POST['fName']; // DON'T do this in real life, It leaves you open to SQL injection
$lName = mysqli_real_escape_string($_POST['lName']); // do it this way to help avoid SQL injection

$query = "INSERT INTO test_people (fName, lName) VALUES ('$fName', '$lName')"; 
echo $query; // shows the assembled SQL Comment it out when it works.
if(mysqli_query($link, $query)){ 
echo "Data entered successfully!"; 
} else {echo "no data entered";}



Good luck,

Options: ReplyQuote

Written By
October 01, 2017 03:08AM
Re: Connecting myPHPadmin to web page
October 03, 2017 10:23PM

Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.