Menu
All I want to do is make a form where you can enter the name of your animal, the type of animal, and it’s age. I’m using mySQL and I have the table made up and I inserted four animals already but I don’t know how to insert the information using a form. Can I have an example of how to do this? Sorry I’m new to this ?
<i>
</i><form action="process.php" method="post"><div>
Animal: <input type="text" name="animal"><br>
Age: <input type="text" name="age"><br>
Name: <input type="text" name="name"><br>
<input type="submit" value="Process">
[code=php]
<?php
foreach($_POST as $post){
if(!isset($post)){
echo "You must fill in all fields.";
exit;
}
}
$db = mysql_connect("host", "username", "pass") or die("Could not connect to database: ". mysql_error());
mysql_select_db("database_name") or die("Could not select database: ". mysql_error());
if(!$db){echo "Unable to connect to DB: ". mysql_error(); exit;}
if(!mysql_select_db("database_name")){echo "Unable to select database: " . mysql_error(); exit;}
$query = "INSERT INTO tablename
(animal
,age
,name
) VALUES ( '". $_POST["animal"] ."', '". $_POST["age"] ."', '". $_POST["name"] ."')";
$result = mysql_query($query);
if(!$result){ echo("Mysql error: ". mysql_error()); exit; } else {
echo("Success!");
}
?>
[/code]
[code=php]
<?php
$con = mysql_connect("localhost", "user", "pass");
if(!$con) echo mysql_error();
$sel = mysql_select_db("test_db");
if(!$sel) {
$create = mysql_create_db("test_db");
if(!$create) echo mysql_error();
}
$query = "INSERT INTO test_table
(name
, e-mail
, age
) VALUES ('Paul Junior', '[email protected]', '15')";
$result = mysql_query($query);
echo (!$result) ? mysql_error() : "DB update successful.";
?>
[/code]
[i]Originally posted by Jona [/i]
[B][font=arial]It's a management system. Instead of using PHP to create your MySQL databases and tables, you can login and customize it with a form in phpMyAdmin. It is [b]much[/b] easier - saves hours. ?[/font] [/B][/QUOTE]
[i]Originally posted by The Cheat [/i]
[B]if you need any help setting it up, let me know and i'll try to help. (it seems confusing at first, but the truth is you only need to edit about 5 variables in a config file to get it working properly...)[/B][/QUOTE]
[i]Originally posted by Haunted [/i]
[B]Let's say I have a database and it contains numbers. Can I create a form where you can type in numbers and hit 'process' then have the number I typed in added to one of the numbers in the database, so if I have the number 4 in the database, I can type 3 and then update it so that the new value is 7. Sorry if this is a stupid question [/B][/QUOTE]
[i]Originally posted by Paul Jr [/i]
[B]I have a question about passwords, now. What would be a secure way of storing passwords in a DB? Converting them with the md5 function, or something equivalent, and then inserting them into the DB? How would that work with password retrieval? [/B][/QUOTE]
[code=php]
echo md5($_POST["pass"]);
[/code]
[code=php]
$query = "SELECT * FROM test_table
ORDER BY auto_increment_field
ASC";
[/code]
[i]Originally posted by Jona [/i]
[B][font=arial]Y'know what, Paul? I've never even looked at phpMyAdmin documentation, so I wouldn't know where to find any; I have asked a few questions (bow to the almighty Mod, pyro) about it, and the rest (as usual) I just tested different things and figured it out. So, if something doesn't work, try option B, if option B fails as well, go for option C. Thing is, you'll almost always have at least four options, and one of them will almost always work - unless, of course, it has nothing to do with your problem. ? [/font] [/B][/QUOTE]
[code=php]
function errMsg($msg) {
echo $msg;
include ("footer.php");
exit;
}
[/code]
[i]Originally posted by Jona [/i]
[B][font=arial]Include your footer files first, then exit. I usually use an exit function of my own...[/font]
[code=php]
[/B][/QUOTE]
function errMsg($msg) {
echo $msg;
include ("footer.php");
exit;
}
[/code]
[i]Originally posted by Jona [/i]
[B][font=arial]Also, you should just overwrite a field to determine when someone last visited.[/font] [/B][/QUOTE]
[code=php]
echo("</body></html>"); exit;
[/code]
[i]Originally posted by Jona [/i]
[B][font=arial]Why check if it's empty? If it is empty, then it won't be equal to the current IP address, now will it?
[/B][/QUOTE]
[i]Originally posted by Jona [/i]
[B]You can also just echo out the end of the page before you exit, if you're not using includes (which probably isn't a great idea).[/font]
[code=php]
[/B][/QUOTE]
echo("</body></html>"); exit;
[/code]
[code=php]
exit("<p>You cannot post more than once every 30 seconds.</p>n</body>n</html>");
[/code]
[code=php]
echo "<p>You cannot post more than once every 30 seconds.</p>n";
echo "</body>n</html>";
exit;
[/code]
[i]Originally posted by Paul Jr [/i]
[B][color=red]Edit:[/color]
I did get it to work, but the date that I'm inserting is created with the [FONT=courier new]date();[/FONT] function, which I don't want. I want to use timestamps, because later, when I get better at this, I want to be able to compare times so the time doesn't update every time you refresh the page. [/B][/QUOTE]
[i]Originally posted by Jona [/i]
[B][font=arial]You can, but I'm not going to do it for you. ? [/font] [/B][/QUOTE]
[i]Originally posted by Jona [/i]
[B][font=arial]I kind of guessed what it looked like... :p [/font] [/B][/QUOTE]
[i]Originally posted by Paul Jr [/i]
[B]Now it's kinda changed... now the ID cell only contains the ID number, and I added a cell to the end containing just a radio button for deleting the row. [/B][/QUOTE]
[i]Originally posted by Jona [/i]
[B][font=arial]And I need to know this, because...? It will help to keep me intellectually balanced while I spray mosquitos in my grandmother's back yard? It will prevent any in-grown toenails for the next year? It will make my future wife become beautiful?[/font] [/B][/QUOTE]
[code=php]
<?php
mysql_connect("localhost", "user", "pass");
mysql_select_db("test_db");
$query = mysql_query("SELECT * FROM test
ORDER BY id
");
if((!isset($_GET["action"])) || ($_GET["action"] != "delete") || ($_GET["action"] != "update")) { ?>
<h1>Links list</h1>
<form action="index.php?action=process" method="post">
<div>
<table>
<thead><tr><th>Category</th><th>Name</th><th><abbr title="Uniform Resource Locator">URL</abbr></th><th>Description</th><th>ID</th><th>Delete</th></tr></thead>
<tbody>
<?php
while($rows = mysql_fetch_array($query)) {
echo "<tr><td>" . ucfirst($rows["cat"]) . "</td><td>" . $rows["name"] . "</td><td><a href="" . $rows["url"] . "">" . $rows["url"] . "</a></td><td>" . $rows["desc"] . "</td><td>" . $rows["id"] . "</td><td><input type="radio" name="delete" value="" . $rows["id"] . "" /></td></tr>n";
} ?>
</tbody></table>
<div style="text-align: right; margin-top: 0.5em;"><input type="submit" name="submit_d" id="submit_d" value="Delete Row" /></div>
</div>
</form>
<form action="index.php?action=update" method="post">
<div>
<h2>Add Link</h2>
<div><label for="cat">Category:</label>
<input type="text" name="cat" id="cat" maxlength="10" /></div>
<div><label for="url">URL:</label>
<input type="text" name="url" id="url" maxlength="40" /></div>
<div><label for="name">Name:</label>
<input type="text" name="name" id="name" maxlength="35" /></div>
<div><label for="desc">Description:</label>
<input type="text" name="desc" id="desc" maxlength="100" /></div>
<input type="submit" name="update" id="update" value="Update Table" />
</div>
</form>
<?php
}
elseif($_GET["action"] == "delete") {
mysql_query("DELETE FROM test
WHERE id
= '" . $_POST["delete"] . "'");
header("location: index.php");
}
elseif($_GET["action"] == "update") {
$cat = htmlspecialchars(strip_tags(trim($_POST["cat"])), ENT_QUOTES);
$url = htmlspecialchars(strip_tags(trim($_POST["url"])), ENT_QUOTES);
$name = htmlspecialchars(strip_tags(trim($_POST["name"])), ENT_QUOTES);
$desc = htmlspecialchars(strip_tags(trim($_POST["desc"])), ENT_QUOTES);
/*if(empty($cat) || empty($url) || empty($name) || empty($desc)) {
header("location: index.php");
exit;
}*/
mysql_query("INSERT INTO test
(cat
, url
, name
, desc
) VALUES ('" . $cat . "', '" . $url . "', '" . $name . "', '" . $desc . "')");
header("location: index.php");
}
?>
[/code]
[i]Originally posted by The Cheat [/i]
[B]i personally prefer using AND and OR in my if statements over using && and ||. I think reading AND and OR makes more sense, especially since my editor's syntax highlighting makes them a diff color and bold. Just my personal prefs ? [/B][/QUOTE]
[i]Originally posted by The Cheat [/i]
[B]so basicly, the easy way would be to execute what you are going to want your script to do through phpmyadmin, then once you've executed it, at the top it will tell you the code it used to execute it, then click "create php code" and then copy it into your php script where desired. Get it? [/B][/QUOTE]
[i]Originally posted by The Cheat [/i]
[B]i personally prefer using AND and OR in my if statements over using && and ||. I think reading AND and OR makes more sense, especially since my editor's syntax highlighting makes them a diff color and bold. Just my personal prefs ? [/B][/QUOTE]
0.1.9 — BETA 6.2