Write a PHP script that obtains a URL and its description from a user and stores
ID: 3535235 • Letter: W
Question
Write a PHP script that obtains a URL and its description from a user and stores the information into a database using MySQL. Create and run a SQL script with a database named URL and a table named Urltable. The first field of the table should countain 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.
Explanation / Answer
<html>
<body>
<?php
mysql_connect("HOST_NAME","USERNAME","PASSWORD");
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("HOST_NAME","USERNAME","PASSWORD");
mysql_select_db("url");
$url = " www.deitel.com";
$url= str_replace("'","'",$url);
$desc = "Cool site!";
$desc = str_replace("'","'",$desc);
mysql_query("INSERT INTO urltable (url,descrip) VALUES ('$url','$desc')");
$url = " www.php.net";
$url= str_replace("'","'",$url);
$desc = "The official PHP site";
$desc = str_replace("'","'",$desc);
mysql_query("INSERT INTO urltable (url,descrip) VALUES ('$url','$desc')");
?>
<?php
$username="username";
$password="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_numrows($result);
mysql_close();
?>
<table border="0" cellspacing="2" cellpadding="2">
<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,"field1");
$f2=mysql_result($result,$i,"field2");
?>
<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++;
}
?>
</body>
</html>
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.