Results 1 to 5 of 5

Thread: simple ( insert and get ) php example .

  1. #1
    Join Date
    Apr 2009
    Posts
    45
    Thanks
    18
    Thanked 0 Times in 0 Posts

    Default simple ( insert and get ) php example .

    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

    Code:
    <?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



    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);
    
    ?>
    
    
    <br /><br /><hr />
    
    <?=$a1[MainPage]?>
    Last edited by egturnkey; 05-13-2009 at 11:48 AM. Reason: i've understood wt i've missed , really thanks so much

  2. #2
    Join Date
    Sep 2008
    Location
    Bristol - UK
    Posts
    842
    Thanks
    32
    Thanked 132 Times in 131 Posts

    Default

    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 Code:
    <?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:

    PHP Code:
    $_POST['variable']; 
    Now for the other code:

    PHP 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.

  3. The Following User Says Thank You to Schmoopy For This Useful Post:

    egturnkey (05-13-2009)

  4. #3
    Join Date
    Apr 2009
    Posts
    45
    Thanks
    18
    Thanked 0 Times in 0 Posts

    Default

    thanks so much

    but can you please give me example how to write the following code


    PHP Code:
    echo $a1['MainPage'] . '<br />'

    within the HTML content like the following


    Code:
    <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 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 />'

    ?>

    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 .

  5. #4
    Join Date
    Apr 2009
    Location
    Cognac, France
    Posts
    400
    Thanks
    2
    Thanked 57 Times in 57 Posts

    Default

    Try this

    PHP Code:
    <textarea name=privacy rows=4 cols=40><?echo $a1[MainPage]?></textarea>

  6. The Following User Says Thank You to forum_amnesiac For This Useful Post:

    egturnkey (05-13-2009)

  7. #5
    Join Date
    Sep 2008
    Location
    Bristol - UK
    Posts
    842
    Thanks
    32
    Thanked 132 Times in 131 Posts

    Default

    When accessing array items you should use single quotes.

    For example if we have this array:

    PHP Code:
    $a1 = array('MainPage'=>'Heres some test text'); 
    To access this data item we should use:

    PHP Code:
    $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:

    PHP Code:
    <?=$variable?>
    Although acceptable on some servers, won't work on others. To be sure this will work whatever the situation you should use:

    PHP Code:
    <?php echo $variable?>
    Also when you write attributes for HTML you should put double quotes around values:

    HTML Code:
    <textarea name="privacy" rows="4" cols="40">
    Ok and now the whole thing, fixed:

    PHP Code:
    <?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.

  8. The Following User Says Thank You to Schmoopy For This Useful Post:

    egturnkey (05-13-2009)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •