1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Need help to insert data into a table from admin painel

Discussion in 'PHP & Perl' started by David_Nich, Feb 18, 2013.

  1. David_Nich

    David_Nich Junior Member

    Joined:
    Nov 2, 2012
    Messages:
    133
    Likes Received:
    71
    Occupation:
    Make Money
    Location:
    Nashville, TN
    Hi,

    I just can't see what's wrong when i try to insert data from my website admin into my database:

    Code:
    <?php
    
    if(!isset($_POST["submit"]))
    {
        $content = "<form method='post' action=''><table border='0' cellspacing='5' cellpadding='0'>
        <tr><td>Equipa 1:</td><td><input type='text' name='hometeam' size='30' /></td></tr>
        <tr><td>Equipa 2:</td><td><input type='text' name='awayteam' size='30' /></td></tr>
        <tr><td>Odd equipa 1:</td><td><input type='text' name='ratiohome' size='30' /></td></tr>
        <tr><td>Odd equipa 2:</td><td><input type='text' name='ratioaway' size='30' /></td></tr>
        <tr><td>Odd empate:</td><td><input type='text' name='ratiox' size='30' /></td></tr>
        <tr><td>ID jogo:</td><td><input type='text' name='sportid' size='30' /></td></tr>
        <tr><td>NADA:</td><td><input type='text' name='played' size='30' /></td></tr>
        <tr><td>Hora:</td><td><input type='text' name='starttime' size='30' /></td></tr>
        <tr><td>NADA:</td><td><input type='text' name='winner' size='30' /></td></tr>
        <tr><td>NADA:</td><td><input type='text' name='totalbets' size='30' /></td></tr>
        <tr><td></td><td><input type='submit' name='submit' value='Enviar' /></td></tr>
        </table></form>";
    }
    else
    {
        if(!empty($_POST["hometeam"]) && !empty($_POST["awayteam"]) && !empty($_POST["ratiohome"]) && !empty($_POST["ratioaway"]) && !empty($_POST["ratiox"]) && !empty($_POST["sportid"]) && !empty($_POST["played"]) && !empty($_POST["starttime"]) && !empty($_POST["winner"]) && !empty($_POST["totalbets"]))
        {
            $hometeam = $_POST["hometeam"];
            $awayteam = $_POST["awayteam"];
            $ratiohome = $_POST["ratiohome"];
            $ratioaway = $_POST["ratioaway"];
            $ratiox = $_POST["ratiox"];
            $sportid = $_POST["sportid"];
            $played = $_POST["played"];
            $starttime = $_POST["starttime"];
            $winner = $_POST["winner"];
            $totalbets = $_POST["totalbets"];
    
            
            $sql = "INSERT INTO `events` (`hometeam`, `awayteam`, `ratiohome`, `ratioaway`, `ratiox`, `sportid`, `played`, `starttime`, `winner`, `totalbets`) VALUES('$hometeam', '$awayteam', '$ratiohome', '$ratioaway', '$ratiox', '$sportid', '$played', '$starttime', '$winner', '$totalbets')";
            $res = mysql_query($sql) or die();
            print("<meta http-equiv='refresh' content='0;URL=admin.php?page=add_bettings' />");
        }
        else
        {
            $template->assign("content", "Texto qql!");
        }
    }
    
    ?>

    I don't get any error, but the data is not added into the events table.

    if anyone could help would be great
     
    Last edited: Feb 18, 2013
  2. bpmik

    bpmik Newbie

    Joined:
    Feb 4, 2013
    Messages:
    45
    Likes Received:
    8
    What is the error code?

    Try to replace this line

    Code:
    $res = mysql_query($sql) or die();
    With this

    Code:
    $res = mysql_query($sql);
    if(!$res)
    {
      $message = 'Query Error:  ' . mysql_error() . '<br />';
      $message .= 'Query supplied: ' . $sql . '<br />';
      die($message);
    }
    I think you will find the query you expect to be submitting is not the same as what is actually being submitted.

    If that change at least gives you more information on the problem,hopefully that helped.

    If not, put prints in all the if/else statements to see where each decision is taking your code as it runs.
     
  3. David_Nich

    David_Nich Junior Member

    Joined:
    Nov 2, 2012
    Messages:
    133
    Likes Received:
    71
    Occupation:
    Make Money
    Location:
    Nashville, TN
    i tried but nothing still..
    i am willing to pay anyone who can fix a few problems on my script.. if you want just give ur skype
     
  4. cgimaster

    cgimaster Power Member

    Joined:
    Jun 30, 2012
    Messages:
    525
    Likes Received:
    311
    Gender:
    Male
    Try this one and let me know how it goes for you, don't forget to replace the database information at the top.

    Code:
    <?php
    
    # This is the information about your MySQL server
    # without this information you will not connect to the database
    # hence no data will be inserted
    $host = 'localhost';
    $username = 'your MySQL username';
    $password = 'your username password';
    $database  = 'your database name';
    
    if(!isset($_POST["submit"]))
    {
        $content = "<form method='post' action=''><table border='0' cellspacing='5' cellpadding='0'>
        <tr><td>Equipa 1:</td><td><input type='text' name='hometeam' size='30' /></td></tr>
        <tr><td>Equipa 2:</td><td><input type='text' name='awayteam' size='30' /></td></tr>
        <tr><td>Odd equipa 1:</td><td><input type='text' name='ratiohome' size='30' /></td></tr>
        <tr><td>Odd equipa 2:</td><td><input type='text' name='ratioaway' size='30' /></td></tr>
        <tr><td>Odd empate:</td><td><input type='text' name='ratiox' size='30' /></td></tr>
        <tr><td>ID jogo:</td><td><input type='text' name='sportid' size='30' /></td></tr>
        <tr><td>NADA:</td><td><input type='text' name='played' size='30' /></td></tr>
        <tr><td>Hora:</td><td><input type='text' name='starttime' size='30' /></td></tr>
        <tr><td>NADA:</td><td><input type='text' name='winner' size='30' /></td></tr>
        <tr><td>NADA:</td><td><input type='text' name='totalbets' size='30' /></td></tr>
        <tr><td></td><td><input type='submit' name='submit' value='Enviar' /></td></tr>
        </table></form>";
    }
    else
    {
        if(!empty($_POST["hometeam"]) && !empty($_POST["awayteam"]) && !empty($_POST["ratiohome"]) && !empty($_POST["ratioaway"]) && !empty($_POST["ratiox"]) && !empty($_POST["sportid"]) && !empty($_POST["played"]) && !empty($_POST["starttime"]) && !empty($_POST["winner"]) && !empty($_POST["totalbets"]))
        {
            $hometeam = $_POST["hometeam"];
            $awayteam = $_POST["awayteam"];
            $ratiohome = $_POST["ratiohome"];
            $ratioaway = $_POST["ratioaway"];
            $ratiox = $_POST["ratiox"];
            $sportid = $_POST["sportid"];
            $played = $_POST["played"];
            $starttime = $_POST["starttime"];
            $winner = $_POST["winner"];
            $totalbets = $_POST["totalbets"];
    
            try
           {
               $conn = new PDO('mysql:host=' . $host . ';dbname=' . $database, $username, $password);
               $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
               $stmt = $conn->prepare('INSERT INTO `events` (`hometeam`, `awayteam`, `ratiohome`, `ratioaway`, `ratiox`, `sportid`, `played`, `starttime`, `winner`, `totalbets`) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)');
               // Use PDO::PARAM_INT for numbers entry and PDO::PARAM_STR for string, dates etc
               $stmt->bindParam(1, $hometeam, PDO::PARAM_STR);
               $stmt->bindParam(2, $awayteam, PDO::PARAM_STR);
               $stmt->bindParam(3, $ratiohome, PDO::PARAM_INT);
               $stmt->bindParam(4, $ratioaway, PDO::PARAM_INT);
               $stmt->bindParam(5, $ratiox, PDO::PARAM_INT);
               $stmt->bindParam(6, $sportid, PDO::PARAM_INT);
               $stmt->bindParam(7, $played, PDO::PARAM_INT);
               $stmt->bindParam(8, $starttime, PDO::PARAM_STR);
               $stmt->bindParam(9, $winner, PDO::PARAM_INT);
               $stmt->bindParam(10, $totalbets, PDO::PARAM_INT);
               $stmt->execute();
    
                print("<meta http-equiv='refresh' content='0;URL=admin.php?page=add_bettings' />");
           }
           catch(PDOException $e)
           {
                echo 'ERROR: ' . $e->getMessage();
           }
        }
        else
        {
            $template->assign("content", "Texto qql!");
        }
    }
    
     
    Last edited: Feb 20, 2013
  5. nickcarter

    nickcarter Newbie

    Joined:
    Feb 19, 2013
    Messages:
    15
    Likes Received:
    3
    Its imposible to insert any data without connection to your mysql database

    Use :
    // connection to db
    $con = mysql_connect("servername","username","password");

    // selecting DB
    mysql_select_db("my_db", $con);
     
  6. SacDin

    SacDin Newbie

    Joined:
    Feb 25, 2013
    Messages:
    4
    Likes Received:
    0
    I can help you on this if you're willing to pay. Please send me info by PM.
     
  7. 0perat0r

    0perat0r Newbie

    Joined:
    Mar 12, 2012
    Messages:
    18
    Likes Received:
    6
    Occupation:
    Journalist
    Home Page:
    Try removing "$res =" or
    You can try changing
    to
    and the form action like that
    that should work