How To Create Teleprompter Web Base II
12:51 PM
submit form |
1. Isi.php is a form to enter data into database.
2. Addtele.php is the liasion between the for content by database
Create Input Form to Database for User
<form class="form" method="post" action="addtele.php"> <p class="judul"> <input type="text" name="judul" id="judul" value="<?php echo isset($_POST['judul']) ? $_POST['judul'] : '';?>" /> <label for="judul">Judul</label> </p> <p class="acara"> <input type="acara" name="acara" id="acara" value="<?php echo isset($_POST['acara']) ? $_POST['acara'] : '';?>"/> <label for="acara">Acara</label> </p> <p class="creator"> <input type="creator" name="creator" id="creator" value="<?php echo isset($_POST['creator']) ? $_POST['creator'] : '';?>" /> <label for="creator">Author</label> </p> <p class="isi"> <textarea name="isi" value="<?php echo isset($_POST['isi']) ? $_POST['isi'] : '';?>"></textarea> </p> <p class="submit"> <input type="submit" value="Kirim" /> </p> </form>
To remeber is the parameter name must be the same as the column name in database. Input with parameter name judul will be stored in a column judul also. If the name doesn’t match with the database then eror messages will appear, variable not recognized. To enter data in a database, I need action from the form that i’ve saved on a file addtele.php that Contains functions for connecting to the database, then choose database that will be included, after that create query MySQL and check out their work or whether the input to a database. Let’s we create file addtele.php.
Connecting Form to Database
The first, We define variable for establish a connection to a database withsession_start(); define('DB_HOST', 'localhost'); define('DB_USER', 'userdb'); define('DB_PASSWORD', 'passdb'); define('DB_DATABASE', 'namadb');After defining a variable required to login, then connect to the database with
$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD); if(!$link) { die('Gagal koneksi dengan server: ' . mysql_error()); }If they fail we will get error message failed connect to server. Usually this happens if variable for the connection to the database is false. Like database name, database username, password. If this is succesfull we are continuing with the function for choose the database.
$db = mysql_select_db(DB_DATABASE); if(!$db) { die("Database tidak dapat dipilih"); } function clean($str) { $str = @trim($str); if(get_magic_quotes_gpc()) { $str = stripslashes($str); } return mysql_real_escape_string($str); }After selecting the database, then we create query MySQL, that is the process input data to database
$qry = "INSERT INTO input(judul, isi, acara, creator) VALUES('".$_POST['judul']."','".$_POST['isi']."','".$_POST['acara']."','".$_POST['creator']."')"; $result = @mysql_query($qry);If we already submit to the database, then we check whether the inputted data already entered to the database with
if($result) { header("location: isi.php"); exit(); }else { die("GAGAL!"); }
If this is succesfull, then we will go into isi.php page again so get the input. If they fail we will get error message failed. Why did throw it to the php page when successful? Because this is the page that is used by the author of the story so we don’t need to have access to the teleprompter page. Confused by the sequence? This is the contents of addtele.php file
<?php session_start(); define('DB_HOST', 'localhost'); define('DB_USER', 'mmb'); define('DB_PASSWORD', 'mmb'); define('DB_DATABASE', 'teleprompter'); //koneksi ke database. $link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD); if(!$link) { die('Gagal koneksi dengan server: ' . mysql_error()); } //pilih database $db = mysql_select_db(DB_DATABASE); if(!$db) { die("Database tidak dapat dipilih"); } function clean($str) { $str = @trim($str); if(get_magic_quotes_gpc()) { $str = stripslashes($str); } return mysql_real_escape_string($str); } //Membuat Query MySQL $qry = "INSERT INTO input(judul, isi, acara, creator) VALUES('".$_POST['judul']."','".$_POST['isi']."','".$_POST['acara']."','".$_POST['creator']."')"; $result = @mysql_query($qry); //Mengecek keberhasilan if($result) { header("location: isi.php"); exit(); }else { die("GAGAL!"); } ?>
NB: Screenshot form above already added with css, so it has the look more interesting than the form that created without css. Please amended if there is a fault of my explanation :D
0 comments