Failed to insert data on mysql

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ddtpmyra
    Contributor
    • Jun 2008
    • 333

    Failed to insert data on mysql

    Hello,

    Im having problem and I dont know why where it coming from, maybe you can help.

    Files:
    create_topic.ph p

    [HTML]<form id="form1" name="form1" method="post" action="add_top ic.php">
    <td>
    <table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFF F">
    <tr>
    <td colspan="3" bgcolor="#E6E6E 6" align="center"> <strong><font size="4">Create New Topic</font></strong> </td>
    </tr>
    <tr>
    <td width="14%"><st rong>Topic</strong></td>
    <td width="2%">:</td>
    <td width="85%"><in put name="topic" type="text" id="topic" size="60" /></td>
    </tr>
    <tr>
    <td valign="top"><s trong>Detail</strong></td>
    <td valign="top">:</td>
    <td><textarea name="detail" cols="50" rows="20" id="detail"></textarea></td>
    </tr>
    <tr>
    <td><strong>Nam e</strong></td>
    <td>:</td>
    <td><input name="name" type="text" id="name" size="50" /></td>
    </tr>
    <tr>
    <td><strong>Ema il</strong></td>
    <td>:</td>
    <td><input name="email" type="text" id="email" size="50" /></td>
    </tr>
    <tr>
    <td>&nbsp;</td>
    <td>&nbsp;</td>

    <td align="center"> <input name="id" type="hidden" id="id" value="<?$id=$_ GET['id']; echo "$id"?>">
    <input type="submit" name="Submit" value="Submit" />
    <input type="reset" name="Submit2" value="Reset" />
    </td> <td>&nbsp; </td>


    </tr>
    </table>
    </td>
    </form>[/HTML]

    File: Add_topic.php

    [PHP]<?php
    $host="localhos t"; // Host name
    $username="xxx" ; // Mysql username
    $password="xxx" ; // Mysql password
    $db_name="cmr"; // Database name
    $tbl_name="foru m_question"; // Table name

    // Connect to server and select database.
    mysql_connect(" $host", "$username" , "$password" )or die("cannot connect");
    mysql_select_db ("$db_name") or die("cannot select DB");

    // get data that sent from form
    $topic=$_POST['topic'];
    $detail=$_POST['detail'];
    $name=$_POST['name'];
    $email=$_POST['email'];
    $id=$_POST['id'];


    $datetime=date( "d/m/y h:i:s"); //create date time

    $sql="INSERT INTO $tbl_name(topic , detail, name, email, datetime, cmrid)
    VALUES('$topic' , '$detail', '$name', '$email', '$datetime','$i d')";
    $result=mysql_q uery($sql);
    if($result){
    echo "<font size='3' face='Times New Roman'>Successf ul</font><br>";
    echo "<p><a href='main_foru m.php?id={$id}' ><font size='4' face='Times New Roman, Times, serif'>View your topic</font></a>";
    }
    else {
    echo "ERROR"; //HERE WHERE QUERY FALL

    }
    mysql_close();
    ?>[/PHP]

    DISPLAY MESSAGE:
    'ERROR'
  • Markus
    Recognized Expert Expert
    • Jun 2007
    • 6092

    #2
    I am not prepared to look through that bundle of code with such a small amount of information. Remember: this is a free forum where users devote their time to help other users. You have to be as helpful as possible or you will not get any help.

    One thing I do suggest is do this with your mysql_query()ie s

    Code:
    $sql = "some sql code";
    mysql_query($sql) or die(mysql_error()); // this line will give an error if there is a problem with the query.

    Comment

    • nathj
      Recognized Expert Contributor
      • May 2007
      • 937

      #3
      Hi,

      Try something like this:

      Code:
      $sql="INSERT INTO $tbl_name(topic, detail, name, email, datetime, cmrid)
      VALUES('" . $topic . "', '" . $detail ."', '" . $name . "', '" . $email. "', '" . $datetime . "', $id)" ; // assuming $id is numeric if not when you create it from the $_POST array wrap it in intVal
      this may help, it may not, I have no idea what the rror is.

      It could be that the variables are not being set at al. I just thought I'd take a guess. do what Markus said - it makes the most sense.

      nathj

      Comment

      • chelvan
        New Member
        • Aug 2008
        • 90

        #4
        hi
        i'm afraid on your if condition.

        here you wish to check that the query was execute or not.

        if so try like this
        before insert count number of rows on your table
        do the query
        count number of rows again on your table

        then check both counter value


        regards
        chel-1

        Comment

        • ddtpmyra
          Contributor
          • Jun 2008
          • 333

          #5
          Originally posted by Markus
          I am not prepared to look through that bundle of code with such a small amount of information. Remember: this is a free forum where users devote their time to help other users. You have to be as helpful as possible or you will not get any help.

          One thing I do suggest is do this with your mysql_query()ie s

          Code:
          $sql = "some sql code";
          mysql_query($sql) or die(mysql_error()); // this line will give an error if there is a problem with the query.

          I tried Markus suggestion and Nathj was right it didn't capture my $id on page that calling my query and here's the error message
          You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near 'id'];?>')' at line 1
          and here's what I did to capture the id value from url I wonder what's wrong with this, everythings is working until I move my script to a Windows Server before it was stored and running on 'Novell' do I have do some more configuration on my httpd?
          [PHP]<input name="id" type="hidden" id="id" value="<? $id=$_GET['id'];?>" />[/PHP]

          Comment

          • pbmods
            Recognized Expert Expert
            • Apr 2007
            • 5821

            #6
            Heya, ddtpmyra.

            Sounds like your PHP code is getting output rather than executed.

            If you are outputting your <input> via PHP, you need to change your syntax:
            [code=php]
            echo '<input ... value="' . $_GET['id'] . '" />';
            [/code]

            To debug your query, try echoing it to make sure you're passing in what you think you're passing in:
            [code=php]echo $sql;[/code]

            To prevent SQL errors, also consider using mysql_real_esca pe_string() (http://php.net/mysql_real_escape_string).

            Comment

            Working...