Jump to content

MySQL inserting with PHp variables


IAWfoc

Recommended Posts

Hey Guys,

 

I have to insert some data in MySQL but it wont work  :shrug:.

 

Please have a look.

 

<?php

// to values are set to empty

$vatsim="";
$ivao="";

// values from form in other page are set
if(isset($_POST["pilotid"])) $pilotid=$_POST["pilotid"];
if(isset($_POST["network"])) $network=$_POST["network"];
if(isset($_POST["vid"])) $vid=$_POST["vid"];
if(isset($_POST["pilot"])) $pilot=$_POST["pilot"];

// if value is that copy data in this value, otherways in that value
if ($network == "IVAO") { $ivao="$vid";}
if ($network == "VATSIM") { $vatsim="$ivao";}

// connect db
include(dbconnect.inc.php);

// first sql to update some data in one table
$sql = "UPDATE `360283`.`jos_users` SET `IPS` = \'1\' WHERE `jos_users`.`id` = \'$pilotid\'";
$result1 = mysql_query($sql);

// 2nd sql to insert some data in other table
$sql2 = "INSERT INTO `360283`.`IPS_Pilots` (`ID`, `Name`, `Hours`, `Flights`, `LastFlight`, `IVAO`, `VATSIM`, `Enabled`, `Rating`) VALUES ('$pilotid', '$pilot', NULL, NULL, NULL, '$ivao', '$vatsim', '1', '0');";
$result2 = mysql_query($sql2);

// sql to check if it was succesful
$sql3 = "SELECT * FROM `IPS_Pilots` WHERE `ID` = '$pilotid'  LIMIT 0, 30 ";
$result3 = mysql_query($sql3);
$num3 = mysql_numrows($result3);


// echo succesfull or not
if (!$num3) { echo "Sorry, but I failed to apply this pilot."; } else { echo "Pilot succesfully applied."; }



?>

 

Thanks

Link to comment
https://forums.phpfreaks.com/topic/218512-mysql-inserting-with-php-variables/
Share on other sites

Thank you I will

 

 

 

What doesnt really work is this:

 

I get to this page and all data is send, when I change the script to echo all vars that goes perfect.

But, it doesn't insert the data in the mysql db.

I think the problems is in the insert and update query.

I do not have any problems with MySQL, A select query goes perfect, even with PHP vats.

But I do not exactly know what the problem is.

 

FOC

You don't need to escape single quotes within a double-quoted string here:

$sql = "UPDATE `360283`.`jos_users` SET `IPS` = \'1\' WHERE `jos_users`.`id` = \'$pilotid\'";

 

You should really add some logic to determine if the UPDATE/INSERT queries executed successfully, and if they actually did anything or not.

$sql2 = "INSERT INTO `360283`.`IPS_Pilots` (`ID`, `Name`, `Hours`, `Flights`, `LastFlight`, `IVAO`, `VATSIM`, `Enabled`, `Rating`) VALUES ('$pilotid', '$pilot', NULL, NULL, NULL, '$ivao', '$vatsim', '1', '0');";
if( $result2 = mysql_query($sql2) ) {
     if( mysql_affected_rows() < 1 ) {
          // The query ran, but nothing was inserted, so this indicates an error.
          echo "No record was inserted.";
     }
} else {
     // the query failed to execute at all, so 
     echo "The query: $sql2<br>Produced an error: " . mysql_error();
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.