View Full Version : Resolved simple ( insert and get ) php example .
egturnkey
05-12-2009, 03:22 PM
Hello Friends,
I've learn get.update.delete but faild in insert command
when i make input of
x1
x2
x3
x4
it stored in database but when i get it
i only get
x1
why
here is the example and let me know wt is the wrong
<?php
require_once('config.inc.php');
if(isset($_POST[s1]))
{
$q1 = "insert into name_table set
MainPage = '$_POST[MainPage]',
AboutUs = '$_POST[AboutUs]' ";
mysql_query($q1) or die(mysql_error());
echo "<br><center>News added successfully!</center><br>";
?>
<form method=post>
<textarea name=MainPage rows=6 cols=35>enter here words</textarea>
<br />
<input type=submit name=s1 value="submit">
</form>
and the index which should have the error need to be fixed is as follow
<?php
require_once('config.inc.php');
//get the current settings
$q1 = "select * from $table_name order by id desc";
$r1 = mysql_query($q1) or die(mysql_error());
$a1 = mysql_fetch_array($r1);
?>
<br /><br /><hr />
<?=$a1[MainPage]?>
Schmoopy
05-12-2009, 08:11 PM
Ok, you should put SQL statements in all CAPITALS. Like so:
SELECT... FROM... WHERE.
Field names should have ticks : `column_1`
So let's edit your code... :
First we have to fix the INSERT INTO statement. You've written it as an UPDATE statement, so that's why it won't work. Here's how it should be:
<?php
require_once('config.inc.php');
if(isset($_POST[s1]))
{
$main = $_POST['MainPage'];
$about = $_POST['AboutUs'];
$q1 = "INSERT INTO name_table (MainPage, AboutUs)
VALUES('$main','$about')";
mysql_query($q1) or die(mysql_error());
echo "<br><center>News added successfully!</center><br>";
?>
I assumed you have two fields called MainPage and AboutUs and we don't really need the ticks here.
$_POST[] variables should also have single quotes around them:
$_POST['variable'];
Now for the other code:
<?php
require_once('config.inc.php');
//get the current settings
$q1 = "SELECT * FROM $table_name ORDER BY `id` DESC";
$r1 = mysql_query($q1) or die(mysql_error());
$a1 = mysql_fetch_array($r1);
while($a1 = mysql_fetch_array($r1)) {
echo $a1['MainPage'] . '<br />';
}
?>
Once you've got the array you want to echo the MainPage for each row.
This should sort it out. Just say if you have any more problems.
egturnkey
05-13-2009, 11:08 AM
thanks so much
but can you please give me example how to write the following code
echo $a1['MainPage'] . '<br />';
within the HTML content like the following
<textarea name=privacy rows=4 cols=40><?=$a1[MainPage]?></textarea>
I've tried but didn't worked so can you please re-coded it so that
<?php
require_once('config.inc.php');
//get the current settings
$q1 = "SELECT * FROM $table_name ORDER BY `id` DESC";
$r1 = mysql_query($q1) or die(mysql_error());
$a1 = mysql_fetch_array($r1);
while($a1 = mysql_fetch_array($r1)) {
echo $a1['MainPage'] . '<br />';
}
?>
separated the data get values than tables connection
thanks in advance really, it helps me so much to understand many things
cause i found the best way to learn anything is to watch an example and compare it with the rules .
forum_amnesiac
05-13-2009, 11:20 AM
Try this
<textarea name=privacy rows=4 cols=40><?echo $a1[MainPage]?></textarea>
Schmoopy
05-13-2009, 11:21 AM
When accessing array items you should use single quotes.
For example if we have this array:
$a1 = array('MainPage'=>'Heres some test text');
To access this data item we should use:
$a1['MainPage'];
If you try to access the array item without quotes it will treat it as a constant and throw an error.
Another thing to note that this way of writing php:
<?=$variable?>
Although acceptable on some servers, won't work on others. To be sure this will work whatever the situation you should use:
<?php echo $variable; ?>
Also when you write attributes for HTML you should put double quotes around values:
<textarea name="privacy" rows="4" cols="40">
Ok and now the whole thing, fixed:
<?php
$a1 = array('MainPage'=>'Hello here\'s some test text');
?>
<textarea name="privacy" rows="4" cols="40"><?php echo $a1['MainPage']; ?></textarea>
Just to note... You can use "\n" within a textarea element to break a line.
Powered by vBulletin® Version 4.2.2 Copyright © 2021 vBulletin Solutions, Inc. All rights reserved.