Jump to content

Problem with writing to MySQL database


ifis

Recommended Posts

I am trying to update a table with a result from another database based off a users selection.  I am having a problem updating the table though.  The scipt does not produce an error message, but I can't figure out why it does not work.

// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");

//Convert male/female to he/she
if ($sex == 'Male'){
$preposition= he;
}
If ($sex == 'Female'){
$preposition= she;
}

$sql = "SELECT * FROM Endorsements WHERE lic='$lic'";
$result=mysql_query($sql);

//get the first entry from the result
$row = mysql_fetch_array($result);

$find = array("\$student","\$make","\$preposition","\$firstName","\$lastName","\$CFI","expire","limitations","departure","destination","\$route","\$airport","landingair","\$airspace","seldate","pilotcertificate","pilotnumber");
$replace = array($student,$make,$preposition,$firstName,$lastName,$CFI,$expireip,$limitations,$departure,$destination,$route,$airport,$landingair,$airspace,$seldate,$pilotcertificate,$pilotnumber);

$dorse =str_replace($find,$replace,$row['end']);
$dorse1 =str_replace($find,$replace,$row['end1']);
$dorse2 =str_replace($find,$replace,$row['end2']);
echo "$dorse</br>";
echo "$dorse1</br>";
echo "$dorse2</br>";

unset($host);
unset($username);
unset($password);
unset($db_name);

include("endorsementlog.inc");

// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");

$today=date("Y/m/d");

mysql_query("UPDATE Endorsementlog SET end='$dorse' AND end1='$dorse1' AND end2='$dorse2' WHERE Student='$student' AND Endorsement='$lic' AND Date='$today'") or die("Cannot inset into Endorsementlog table. MySQL Returned: ".mysql_error());

mysql_close();

It's probably something stupid, but I'm stuck.  Thanks

Link to comment
Share on other sites

Why do you unset the usersnamd and password before using them? Also, you don't need quotes around your variables in the connect function and others like that.

 

Secondly, "Doesn't Work" is too vague.

 

Where is $student defined?

Link to comment
Share on other sites

I unset the variables because I am connecting to a different database and did not want the original variables that I used to connect to the first database to interfere with the connection with my second database.  Basically, I want to get a string that is saved in one database, update the strings variables with inputs from a form and then save the resulted in another database.  The script runs without an error message, but does not write anything to the MySQL database.  Here is the first part of it

//get data
$make=$_POST['make'];
$lic = $_POST['lic'];
$student=$_POST['student'];
$sex = $_POST['sex'];
$CFI=$_POST['CFI'];
$expireip=$_POST['expireip'];
$firstName=$_POST['firstName'];
$lastName=$_POST['lastName'];
$limitations=$_POST['limitations'];
$departure=$_POST['departure'];
$destination=$_POST['destination'];
$route=$_POST['route'];
$airport=$_POST['airport'];
$airspace=$_POST['airspace'];
$landingair=$_POST['landingair'];
$seldate=$_POST['seldate'];
$pilotcertificate=$_POST['pilotcertificate'];
$pilotnumber=$_POST['pilotnumber'];

include("endorsement.inc");

Link to comment
Share on other sites

I have a mysql_error() and it does not show anything.  Maybe there is a better way to do it.  Here is the code I have:

mysql_query("UPDATE Endorsementlog SET end='$dorse' AND end1='$dorse1' AND end2='$dorse2' WHERE Student='$student' AND Endorsement='$lic' AND Date='$today'") or die("Cannot inset into Endorsementlog table. MySQL Returned: ".mysql_error())

as far as echo the queary, how would I do that just

echo "mysql_query('UPDATE Endorsementlog SET end='$dorse' AND end1='$dorse1' AND end2='$dorse2' WHERE Student='$student' AND Endorsement='$lic' AND Date='$today'') or die('Cannot inset into Endorsementlog table. MySQL Returned: '.mysql_error())";

?

Link to comment
Share on other sites

Put the query in a string as a variable and then print it.

<?php

$sql = "UPDATE Endorsementlog SET end='$dorse' AND end1='$dorse1' AND end2='$dorse2' WHERE Student='$student' AND Endorsement='$lic' AND Date='$today'';

print $sql;

mysql_query($sql) or die('Cannot inset into Endorsementlog table. MySQL Returned: '.mysql_error())";

?>

 

 

Link to comment
Share on other sites

OK, now I got an error: Cannot inset into Endorsementlog table. MySQL Returned: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'Endorsement='Private Pilot Practical Test (Single-Engine)',Date='2008/02/27'' at line 1

here is the code I updated:

mysql_query("UPDATE Endorsementlog SET end='$dorse',end1='$dorse1',end2='$dorse2' WHERE Student='$student',Endorsement='$lic',Date='$today'")or die("Cannot inset into Endorsementlog table. MySQL Returned: ".mysql_error());

Link to comment
Share on other sites

shouldnt brake lol

 

<?php


//database connection 1


$hosta="";
$usernamea="";
$passworda="";
$dbname_a="";

//database connection b.

$hostb="";
$usernameb="";
$passwordb="";
$db_nameb="";

// Connect to server and select databse.

mysql_connect("$hosta", "$usernamea", "$passworda")or die("cannot connect"); 
mysql_select_db("$db_namea")or die("cannot select DB");

//Convert male/female to he/she
if ($sex == 'Male'){

$preposition='he';
}

If ($sex == 'Female'){

	$preposition='she';
}

$sql = "SELECT * FROM Endorsements WHERE lic='$lic'";
$result=mysql_query($sql);

//get the first entry from the result
$row = mysql_fetch_array($result);

$find = array("\$student","\$make","\$preposition","\$firstName","\$lastName",
"\$CFI","expire","limitations","departure","destination","\$route","\$airport",
"landingair","\$airspace","seldate","pilotcertificate","pilotnumber");

$replace = array($student,$make,$preposition,$firstName,$lastName,$CFI,
$expireip,$limitations,$departure,$destination,$route,$airport,$landingair
,$airspace,$seldate,$pilotcertificate,$pilotnumber);

$dorse =str_replace($find,$replace,$row['end']);
$dorse1 =str_replace($find,$replace,$row['end1']);
$dorse2 =str_replace($find,$replace,$row['end2']);
echo "$dorse</br>";
echo "$dorse1</br>";
echo "$dorse2</br>";

include("endorsementlog.inc");

// Connect to server and select databse.
mysql_connect("$hostb", "$usernameb", "$passwordb")or die("cannot connect"); 
mysql_select_db("$db_nameb")or die("cannot select DB");

mysql_query("UPDATE Endorsementlog SET end='$dorse' , end1='$dorse1' , end2='$dorse2'
WHERE Student='$student' AND Endorsement='$lic' AND Date=NOW() ") or die("Cannot inset 
into Endorsementlog table. MySQL Returned: ".mysql_error());

?>

Link to comment
Share on other sites

add $pilotnumber=mysql_real_escape_string($_POST['pilotnumber']);to every varable mate...

 

 

//get data
$make=$_POST['make'];
$lic = $_POST['lic'];
$student=$_POST['student'];
$sex = $_POST['sex'];
$CFI=$_POST['CFI'];
$expireip=$_POST['expireip'];
$firstName=$_POST['firstName'];
$lastName=$_POST['lastName'];
$limitations=$_POST['limitations'];
$departure=$_POST['departure'];
$destination=$_POST['destination'];
$route=$_POST['route'];
$airport=$_POST['airport'];
$airspace=$_POST['airspace'];
$landingair=$_POST['landingair'];
$seldate=$_POST['seldate'];
$pilotcertificate=$_POST['pilotcertificate'];
$pilotnumber=$_POST['pilotnumber'];

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.