PDA

View Full Version : Update MySQL Database via HTML and PHP. [Help]



Jack!
16-11-2013, 11:04 PM
I know this is pretty basic stuff, not done anything with it though for a few years.

I'm looking to update a Database, the structure is like this:

Database
- Website
-- News

Main database > Table > Row.

I know I need to use the UPDATE which I have attempted to do, but no luck, just wondered if anyone can direct me in the right direction.

Cheers.

Edit:

Just to add, this I what I have so far:


<form method="post" action="<?php $_PHP_SELF ?>"><textarea border='1' name='NewsUpdate' maxlength='500' cols='125' rows='10'/></textarea><br><center><input size='15'type='submit' value='update' /></center></form>

Drew
16-11-2013, 11:41 PM
I don't really have experience with PHP, but I do know how to update a database with Python. Are you using MySQL Database? This link looks helpful: http://www.tutorialspoint.com/php/mysql_update_php.htm

Zak
17-11-2013, 10:57 AM
<form action='register.php' method='POST'>
<table>
<tr>
<td>
Enter your first name:
</td>
<td>
<input type='text' name='firstname' value='<?php echo $firstname; ?>'>
</td>
</tr>
<tr>
<td>
Enter your surname:
</td>
<td>
<input type='text' name='surname' value='<?php echo $surname; ?>'>
</td>
</tr>
<tr>
<td>
Enter your username:
</td>
<td>
<input type='text' name='username' value='<?php echo $username; ?>'>
</td>
</tr>
<tr>
<td align="left">
Enter your password:
</td>
<td>
<input type='password' name='password'>
</td>
</tr>
<tr>
<td>
Repeat your password:
</td>
<td>
<input type='password' name='verifypassword'>
</td>
</tr>
</table>
<br>
<input type='submit' name='submit' value='Register'>
</form>
<br>
<?php
$submit = $_POST['submit'];
$firstname = strip_tags($_POST['firstname']);
$surname = strip_tags($_POST['surname']);
$username = strip_tags($_POST['username']);
$password = strip_tags($_POST['password']);
$verifypassword = strip_tags($_POST['verifypassword']);$date = date("Y-m-d");

if ($submit) {
$query = mysql_query("SELECT * FROM users WHERE username='$username'");
$numrows = mysql_num_rows($query);

if($numrows){ die("This user already exists"); }

if($firstname&&$surname&&$username&&$password&&$verifypassword) {
if($password==$verifypassword) {
if(strlen($firstname)>25 || strlen($surname)>25 || strlen($username)>25) {
die("Error: Max character limit exceeded - All fields must be below 25 characters");
} else if(strlen($password) > 25 || strlen($password) < 6) {
die("Error: Your password must be between 6 and 25 characters.");
} else
$password = md5($password);
$verifypassword =md5($verifypassword);

mysql_select_db("yourdb") or die("Error selecting database");
$query = mysql_query("INSERT INTO users VALUES ('','$firstname','$surname','$admin','$username',' $password','$date')");

die("You have been successfully registered");
} else echo "Error: Your passwords do not match.";
} else echo "Error: Please fill in <b>every field</b>";
}?>

A bit like that?

I used this in one of website pages years ago. (sorry about the formatting - just had to rejig it in here -.-)

I know the code is an insert into but UPDATE code is very similar (eg. UPDATE tablename SET field='' WHERE otherfield>2;)

Jack!
17-11-2013, 02:52 PM
if($_POST['submit']){
$_POST['textarea'] = str_replace("'", "&#39;", $_POST['textarea']);
$con->query("UPDATE `website` SET NewsShort = '".$_POST['textarea']."'");
echo "<center><h3>You have changed the news.</h3></center>";
unset($_POST);

}else{
echo "<h2>New News:</h2>
You may add in HTML codes.<br><br>
<form name='input' action='' method='post'>
<textarea border='1' name='textarea' maxlength='500' cols='125' rows='10'/></textarea><br><center><input size='15'type='submit' name='submit'value=' Submit ' /></center>
</form><br>";
$text = $_POST['textarea'];
$submit = $_POST['submit'] = 1;
}

}
?>

Did it like that in the end, just the PHP bit there, the field bit isn't difficult.

Zak
17-11-2013, 05:16 PM
if($_POST['submit']){
$_POST['textarea'] = str_replace("'", "&#39;", $_POST['textarea']);
$con->query("UPDATE `website` SET NewsShort = '".$_POST['textarea']."'");
echo "<center><h3>You have changed the news.</h3></center>";
unset($_POST);

}else{
echo "<h2>New News:</h2>
You may add in HTML codes.<br><br>
<form name='input' action='' method='post'>
<textarea border='1' name='textarea' maxlength='500' cols='125' rows='10'/></textarea><br><center><input size='15'type='submit' name='submit'value=' Submit ' /></center>
</form><br>";
$text = $_POST['textarea'];
$submit = $_POST['submit'] = 1;
}

}
?>

Did it like that in the end, just the PHP bit there, the field bit isn't difficult.

Glad you figured it out. Kinda guessed you would, PHP is a very easy language to understand and manipulate! :D

Want to hide these adverts? Register an account for free!