Write a PHP script that obtains a URL and its description from user and stores t
ID: 3542908 • Letter: W
Question
Write a PHP script that obtains a URL and its description from user and stores the information into a database using MySQL. Create and run a SQL script with database named URL and 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 second 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 a table. [Note: Follow the instructions in Section 18.5.2 to create the Url database by using the URLs.sql script that's provided with this chapter's examples in the dbscripts folder.]
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.