Need help to insert data into a table from admin painel

David_Nich

Junior Member
Joined
Nov 2, 2012
Messages
133
Reaction score
72
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:
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.
 
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
 
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:
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);
 
I can help you on this if you're willing to pay. Please send me info by PM.
 
Back
Top