-1

Hello guys I am trying to apply this query but values are not saving in my database. I have tried a lot but I am not able to fix it. Please Help me. Thanks in advance

if($_POST) {
    $lecturer_id = $_SESSION["lecturer_id"];
    $game_name= $_POST['game_name'];
    mysql_query("insert into games(game_name, lecturer_id) values ('$game_name', '$lecturer_id')");
    $input_type = $_POST['type'];
    if($input_type=='multiple_choice') {
        $question= $_POST['quest'];
        $val1= $_POST['value1'];
        $val2= $_POST['value2'];
        $val3= $_POST['value3'];
        $val4= $_POST['value4'];
        //echo $question;
        // echo $val4;
        mysql_query("insert into subgames(game_id, input_id, statement, option1,    option2, option3, option4) values ((SELECT id 
        FROM games
        WHERE game_name = '$game_name'), '$input_type', '$question', '$val1', '$val2', '$val3', '$val4'");
    } elseif($input_type=='input_field') {
        $question= $_POST['quest'];
        $answer= $_POST['ans'];
    }
} else {
echo "Not Submitted";
}
miken32
  • 42,008
  • 16
  • 111
  • 154
Muhammad Umar
  • 349
  • 3
  • 14
  • Any error messages we can work with? – Dan Nov 04 '15 at 23:26
  • Are you sure you are connecting to the database properly? Could you post the code that connects you to the database? – Sandyandi N. dela Cruz Nov 04 '15 at 23:26
  • 4
    since you are starting out, get into mysqli or pdo, as you will be vulnerable to sql injection attacks with what you certainly have. [How can I prevent SQL-injection in PHP](http://stackoverflow.com/q/60174) ... So just throw it out. Use prepared stmts – Drew Nov 04 '15 at 23:27
  • Have you session_start() before you use $_SESSION? – Kostas Mitsarakis Nov 04 '15 at 23:27
  • Beside your question you need to use Prepared Statements instead of this way of querying you are so open to SQL injection – Mi-Creativity Nov 04 '15 at 23:27
  • No Error messages. And yes session_start() is added. Database connection is working properly as i am using the same database file for all other things and they are working fine – Muhammad Umar Nov 04 '15 at 23:31
  • @dan08 no error messages – Muhammad Umar Nov 04 '15 at 23:38
  • 1
    You _should_ [pick another API](http://docs.php.net/manual/en/mysqlinfo.api.choosing.php) to connect to your mysql server as the mysql_* extension is deprecated not present in the upcoming release of php 7. You _must_ add some kind of error handling. For the mysql_* functions that would be checking each and every return value; if it's FALSE something went wrong in which case mysql_error() can tell you more details. – VolkerK Nov 04 '15 at 23:39
  • @HarishTalanki i am using the same database connection file for the whole project and everything is fine but on this page my values are not getting saved please help me – Muhammad Umar Nov 05 '15 at 00:12
  • Even though you use the same database connection, when you run the DML queries (insert or delete), you need to [commit](http://php.net/manual/en/mysqli.commit.php). – Harish Talanki Nov 05 '15 at 00:18
  • I did not commit and i don't know how to do this. can you please help me? – Muhammad Umar Nov 05 '15 at 00:25
  • Click on "commit" in my previous comment.. It will take to the page. – Harish Talanki Nov 05 '15 at 00:28

2 Answers2

0

Did you try to check the mysql_error()?

Add this code:

echo mysql_error();

Then post the error.

Edmhar
  • 642
  • 1
  • 7
  • 24
  • After applying this(echo mysql_error($query);) i am getting this error Warning: mysql_error() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\class\lecturer\newGame.php on line 216 Add Input – Muhammad Umar Nov 05 '15 at 00:36
  • please help me i am stuck here – Muhammad Umar Nov 05 '15 at 00:43
  • 1
    @MuhammadUmar Follow the instructions better. Look at Edmhar's code vs what you applied. – James Nov 05 '15 at 00:47
  • @MuhammadUmar don't add `$query` just `echo mysql_error();` – Edmhar Nov 05 '15 at 00:51
  • @James i have applied this query on my phpmyadmin and i am getting this error #1452 - Cannot add or update a child row: a foreign key constraint fails (`class`.`games`, CONSTRAINT `games_ibfk_1` FOREIGN KEY (`lecturer_id`) REFERENCES `lecturer` (`id`)) – Muhammad Umar Nov 05 '15 at 00:51
  • @Edmhar i have this query to my phpmyadmin and i am getting this error #1452 - Cannot add or update a child row: a foreign key constraint fails (`class`.`games`, CONSTRAINT `games_ibfk_1` FOREIGN KEY (`lecturer_id`) REFERENCES `lecturer` (`id`)) – Muhammad Umar Nov 05 '15 at 00:54
  • @MuhammadUmar: You're getting this error because you're trying to add/update a row to table that does not have a valid value. It's either `lecturer_id` or `lecturer (id))` doesn't exist in the table. – Edmhar Nov 05 '15 at 00:55
  • Try to check your spelling of your columns and like I said try to run the `echo mysql_error();` – Edmhar Nov 05 '15 at 00:56
  • @Edmhar I have added this and i am getting this error Cannot add or update a child row: a foreign key constraint fails (`class`.`games`, CONSTRAINT `games_ibfk_3` FOREIGN KEY (`game_type_id`) REFERENCES `gametype` (`id`)) – Muhammad Umar Nov 05 '15 at 01:03
  • @MuhammadUmar check the spelling one by one of your columns, tables look if they match, – Edmhar Nov 05 '15 at 01:05
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/94263/discussion-between-muhammad-umar-and-edmhar). – Muhammad Umar Nov 05 '15 at 01:12
  • @James please help me i am stuck – Muhammad Umar Nov 05 '15 at 01:53
0
    Step 1:-
     SHOW CREATE TABLE table_name:

    It will show table details

    | table_name | CREATE TABLE `table_name` (
     `id` int(20) unsigned NOT NULL auto_increment,
     `key_column` smallint(5) unsigned default '1',
      KEY `key_column` (`key_column`),
      CONSTRAINT `table_name_ibfk_1` FOREIGN KEY (`key_column`) REFERENCES 
     `second_table` (`id`) ON DELETE SET NULL
      ) ENGINE=InnoDB DEFAULT CHARSET=utf8 |

     Step2:-
     ALTER TABLE table_name DROP KEY `key_column`;

     Step3:-
     ALTER TABLE table_name DROP FOREIGN KEY `table_name_ibfk_1`;
Muhammad Umar
  • 349
  • 3
  • 14