I have a little problem over here this code doesn't work well what is the problem if anyone know ?

$getUsername = getUserData('Username');
$sql = "INSERT INTO `kladilnica` VALUES ('', '".$getUsername."', '".$betAmouth."', '".$gain."', '".$date."', '".$match1."', '".$match2."', '".$match3."', '".$match4."', '".$match5."', 
'".$match6."', '".$match7."', '".$match8."', '".$match9."', '".$match10."', '".$match11."', '".$match12."', '".$match13."', 
'".$match14."', '".$match15."', '".$match16."', '".$odd."')";

if($conn->query($sql) === false) {
    trigger_error('Wrong SQL: ' . $sql . ' Error: ' . $conn->error, E_USER_ERROR);
} else {
    $last_inserted_id = $conn->insert_id;
    $affected_rows = $conn->affected_rows;
    echo "Ticket added successfully.";
}

And also if someone can tell me how i can insert values into two tables using query ?

Recommended Answers

All 10 Replies

Are you getting an error?

First look says no ending semicolon... Other than that, depends on the table and what values it expects. Try listing out the columns explicitly and see if that helps. I'm also assuming the first column is your id and you may be passing a blank string instead of the primitive null... Again, without an error, it's hard to say.

It is much easier to check the query if you do not use all the concatenation. You can do it this way:

$sql = "INSERT INTO `kladilnica` VALUES ('', '$getUsername', '$betAmouth', '$gain', '$date', '$match1', '$match2', '$match3', '$match4', '$match5','$match6', '$match7', '$match8', '$match9', '$match10', '$match11', '$match12', '$match13', '$match14', '$match15', '$match16', '$odd')";

You have to make sure all the variables are set otherwise you will get an error. It is wise to test the query. Put this code after it:

die($sql);

It will display the query and stop the script. Now you can inspect the query if it is OK and copy it into phpmyadmin to test it.

And a question: are $match1, $match2, $match3... actual fields (columns) in the kladilnica table? Maybe your database design is not scalable.

yup they are 16 input fields and i tryed in the phpmyadmin and it works but with the code i have doesn't work and i do not have any errors, maybe there are too much fields to be updated or something else is the problem ?

Here is the whole script so if anyone know what is wrong here ?
Also when i will delete the matches from the query and let just ID username betamouth gain and date it works fine.

<?php
    if(
    isset($_POST['date']) &&
    isset($_POST['gain']) &&
    isset($_POST['amouth']) &&  
    isset($_POST['match1']) && 
    isset($_POST['match2']) && 
    isset($_POST['match3']) && 
    isset($_POST['match4']) && 
    isset($_POST['match5']) && 
    isset($_POST['match6']) &&
    isset($_POST['match7']) && 
    isset($_POST['match8']) && 
    isset($_POST['match9']) && 
    isset($_POST['match10']) && 
    isset($_POST['match11']) && 
    isset($_POST['match12']) &&
    isset($_POST['match13']) && 
    isset($_POST['match14']) && 
    isset($_POST['match15']) && 
    isset($_POST['match16']) &&
    isset($_POST['koef'])) {
        $match1 = $_POST['match1'];
        $match2 = $_POST['match2'];
        $match3 = $_POST['match3'];
        $match4 = $_POST['match4'];
        $match5 = $_POST['match5'];
        $match6 = $_POST['match6'];
        $match7 = $_POST['match7'];
        $match8 = $_POST['match8'];
        $match9 = $_POST['match9'];
        $match10 = $_POST['match10'];
        $match11 = $_POST['match11'];
        $match12 = $_POST['match12'];
        $match13 = $_POST['match13'];
        $match14 = $_POST['match14'];
        $match15 = $_POST['match15'];
        $match16 = $_POST['match16'];
        $odd = $_POST['koef'];
        $betAmouth = $_POST['amouth'];
        $gain = $_POST['gain'];
        $date = $_POST['date'];

        if (!is_numeric($betAmouth) || !is_numeric($gain))  {
            echo "Must enter a number!";
        } else {
            if(empty($betAmouth) || empty($gain) || empty($date)){
                echo "You can't leave empty fields.";
            } else {
                $getUsername = getUserData('Username');

                $sql = "INSERT INTO `kladilnica` VALUES ('', '".$getUsername."', '".$betAmouth."', '".$gain."', '".$date."', '".$match1."', '".$match2."', '".$match3."', '".$match4."', '".$match5."', 
                '".$match6."', '".$match7."', '".$match8."', '".$match9."', '".$match10."', '".$match11."', '".$match12."', '".$match13."', 
                '".$match14."', '".$match15."', '".$match16."', '".$odd."')";

                if($conn->query($sql) === false) {
                    trigger_error('Wrong SQL: ' . $sql . ' Error: ' . $conn->error, E_USER_ERROR);
                } else {
                    $last_inserted_id = $conn->insert_id;
                    $affected_rows = $conn->affected_rows;
                    echo "Ticket added successfully.";
                }
            }
        }
    }
?>

Here is the whole script

I don't see a database connect. Where is $conn instantiated?

Echo out $sql and post it here.

connect.php

<?php
    $DBServer = 'localhost';
    $DBUser   = 'root';
    $DBPass   = '';
    $DBName   = 'upstrey';

    $conn = new mysqli($DBServer, $DBUser, $DBPass, $DBName);

    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    }
?>

But i think this is not the problem because like i said when i will remove the matches the code works just fine...

EDITED: the match fields are in other <form> maybe that is the problem ?
i putted in other form because it was eazyer to place in position if someone know better way of doing this please share it :)

<form action="ticket.php" method="POST" style="position: absolute; margin-left: 15px;">
                        <a id="title_text"><b>Sport Ticket</b></a>
                        <p><input id="mAmouth" type="text" name="amouth" placeholder="Amount Of Payment?"/></p>
                        <input id="mAmouth" type="text" name="gain" placeholder="Gain?"/>

                        <p><input type="text" name="date" id="datepicker" placeholder="Date?"><input type="text" name="koef" id="koef" placeholder="Odds?"></p>
                        <input id="place_ticket" type="submit" value="Place Ticket" onclick="myFunction()"/><br><br>
                    </form>
                    <form method="POST" style="position: relative; margin-left: 325px;">
                        <input type="text" name="match1" id="match" placeholder="Match?">
                        <input type="text" name="match2" id="match" placeholder="Match?"><br>
                        <input type="text" name="match3" id="match" placeholder="Match?">
                        <input type="text" name="match4" id="match" placeholder="Match?"><br>
                        <input type="text" name="match5" id="match" placeholder="Match?">
                        <input type="text" name="match6" id="match" placeholder="Match?"><br>
                        <input type="text" name="match7" id="match" placeholder="Match?">
                        <input type="text" name="match8" id="match" placeholder="Match?"><br>
                        <input type="text" name="match9" id="match" placeholder="Match?">
                        <input type="text" name="match10" id="match" placeholder="Match?"><br>
                        <input type="text" name="match11" id="match" placeholder="Match?">
                        <input type="text" name="match12" id="match" placeholder="Match?"><br>
                        <input type="text" name="match13" id="match" placeholder="Match?">
                        <input type="text" name="match14" id="match" placeholder="Match?"><br>
                        <input type="text" name="match15" id="match" placeholder="Match?">
                        <input type="text" name="match16" id="match" placeholder="Match?"><br>
                    </form>

It could be that the kladilnica table is not matched to the field types. Can you post the structure of the table (use SHOW CREATE TABLE kladilnica in phpmyadmin).

And please note: you should sanitize user supplied variables before inserting them into the database.

yup i know that but i dont know the way of doing that in mysqli. When i type that into SQl in phpmyadmin i get small table with this results how can i show the results you are asking for ?

kladilnica
CREATE TABLE `kladilnica` (
 `ID` int(11) NOT NUL...

Opss the problem was in the <form> i delited the form with the matches fields and now works fine... but now i have other problem i do not know how to place the fields in the position i want to be and thats the right side of the box

Here is the box im using right now http://jsfiddle.net/

JSFIDDLE shows no code. Anyway, since this is more a CSS question, I suggest you start a new thread in the Web Design, HTML and CSS Discussion part of DW. This way you will have other people looking at it (I guess not many people look at threads with so many posts).

Once this thread is finished, please mark it as solved.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.