19.9 Write a PHP script that obtains a URL and its description from a user and s
ID: 3690307 • Letter: 1
Question
19.9 Write a PHP script that obtains a URL and its description from a user and stores the information into a databaseusing MySQL. create and run a SQL script with a database named URLand a table named Urltable. the first field of the table should contain an actual URL, and the second, which is named description, should contain a description of the URL. Use www.deitel.com as the first URL, and input COOL Site! as its description. The ssecon URL should be www.php.net, and the description should be The Official PHP site. After each new URL is submitted, print the contents of the database in the table. Please include the following three files for your solution (.html, .php, and .sql).
Explanation / Answer
<html>
<body>
<?php
if(isset($_POST["url"]))
{
mysql_connect("localhost","root","");
//mysql_query("CREATE DATABASE `url` DEFAULT CHARACTER SET latin1 COLLATE latin1_swedish_ci");
mysql_select_db("url");
mysql_query("
CREATE TABLE IF NOT EXISTS `urltable` (
`url` varchar(1000) NOT NULL,
`descrip` varchar(1000) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1"
);
?>
<?php
mysql_connect("localhost","root","");
mysql_select_db("url");
$url = $_POST["url"];
$url= str_replace("'","'",$url);
$desc = $_POST["des"];
$desc = str_replace("'","'",$desc);
mysql_query("INSERT INTO urltable (url,descrip) VALUES ('$url','$desc')");
?>
<?php
$username="root";
$password="";
$database="url";
mysql_connect("localhost",$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
$query="SELECT * FROM urltable";
$result=mysql_query($query);
$num=mysql_num_rows($result);
mysql_close();
?>
<table border="1" cellspacing="4" cellpadding="4">
<tr>
<td><font face="Arial, Helvetica, sans-serif">Value1</font></td>
<td><font face="Arial, Helvetica, sans-serif">Value2</font></td>
</tr>
<?php
$i=0;
while ($i < $num) {
$f1=mysql_result($result,$i,"url");
$f2=mysql_result($result,$i,"descrip");
?>
<tr>
<td><font face="Arial, Helvetica, sans-serif"><?php echo $f1; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><?php echo $f2; ?></font></td>
</tr>
<?php
$i++;
}}
else
{
?>
<div>
<form name="subm" action="test.php" method="POST">
<label for="url">URL:</label><input type="text" name="url" />
<label for="desc"style="float: top;">Description:</label><input type="text" name="des">
<input type="submit" name="sub" />
</form>
</div>
<?php
}
?>
</body>
</html>
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.