Jump to content

[SOLVED] newbie cant get INSERT into SQL to work


garty

Recommended Posts

Hi all. Real newbie here to PHP (like started this week) although been programming VB6 for years.

 

I host my own web server on XP machine running Abyss web server, PHP5 and MS SQL 2000 personal edition.

 

All I am trying to do (to begin with) is have a contact page write it's information to the SQL database which will be picked up by a VB6 front end on another machine.

 

I have tried cutting and pasting from various websites and have had success in some areas. I am building up test pages adding something and running it on a browser to test then adding more etc.

 

This is what I have done so far..

 

test2.php is simple web form with 4 named elements and a submit button using POST

<html>
<body>

<form name="input" action="process2.php" method="POST">

Type your name: 
<input type="text" name="namefield" value="" size="20">
<br>Type your contact number: 
<input type="text" name="numberfield" value="" size="20">
<br>
<br>Type your email: 
<input type="text" name="emailfield" value="" size="20">
<br>
<br>Type your message: 
<TEXTAREA NAME="messagefield", ROWS=5, COLS=20> </TEXTAREA><br><br>

<input type="submit" value="Submit"><br>

</form> 

<p>
If you click the "Submit" button, you will send your input to a new page called process2.php.
</p>

</body>
</html>

 

process2.php picks up the POST data echos it to the page to prove it was picked up. It INCLUDES connect2database.php which stores the connection info. I then ask for a query to be run and it displays on the screen..

<html>
<body>
<?php 
$namefield = $_REQUEST["namefield"]; 
$numberfield = $_REQUEST["numberfield"]; 
$emailfield = $_REQUEST["emailfield"];  
$messagefield = $_REQUEST["messagefield"]; 
?> 

Welcome <?php echo $namefield; ?>.<br />
number <?php echo $numberfield; ?>.<br />
email <?php echo $emailfield; ?>.<br />
message <?php echo $messagefield; ?>.<br />

<?php

//call connection info and connect
include("connect2database.php");

//test connection
if (!$conn)
  {exit("Connection Failed: " . $conn);}
$sql="SELECT * FROM tblContact";
$rs=odbc_exec($conn, $sql);
if (!$rs)
  {exit("Error in SQL");}
echo "<table><tr>";
echo "<th>Name</th>";
echo "<th>Contact</th>";
echo "<th>Email</th>";
echo "<th>Message</th></tr>";
while (odbc_fetch_row($rs))
{
  $conName=odbc_result($rs,"Name");
  $conNum=odbc_result($rs,"Contact");
  $conEmail=odbc_result($rs,"Email");
  $conMsg=odbc_result($rs,"Message");
  echo "<tr><td>$conName</td>";
  echo "<td>$conNum</td>";
  echo "<td>$conEmail</td></tr>";
  echo "<td>$conMqa1,
  sg</td></tr>";
}
odbc_close($conn);
echo "</table>";


?>
</body>
</html>

 

connect2database.php has code.. (details changed from real passwords)

<?php
$myServer = "MyWeb";
$myUser = "sa";
$myPass = "MyWeb";
$myDB = "myweb"; 

//connection to the database
$conn=odbc_connect($myServer, $myUser, $myPass); ?>

 

the process2.php gives me the following information on screen...

Welcome 56.

number 56.

email 56.

message 56.

Name Contact Email Message

bbbb vvvv cccc

cvb

1111 2222 3333

dhhgdgh

which proves it is sucessfully connecting to the SQL database as they are the entries in the database.

 

So I moved to INSERT.. once again my code was built up.

 

test.php

<html>
<body>

<form name="input" action="process.php" method="POST">

Type your name: 
<input type="text" name="namefield" value="" size="20">
<br>Type your contact number: 
<input type="text" name="numberfield" value="" size="20">
<br>
<br>Type your email: 
<input type="text" name="emailfield" value="" size="20">
<br>
<br>Type your message: 
<TEXTAREA NAME="messagefield", ROWS=5, COLS=20> </TEXTAREA><br><br>

<input type="submit" value="Submit"><br>

</form> 

which is the same as before, but points to process.php (not process2.php

 

process.php code is ..

<html>
<body>
<?php $namefield = $_REQUEST["namefield"]; ?> 
<?php $numberfield = $_REQUEST["numberfield"]; ?> 
<?php $emailfield = $_REQUEST["emailfield"]; ?> 
<?php $messagefield = $_REQUEST["messagefield"]; ?> 

Welcome <?php echo $namefield; ?>.<br />
number <?php echo $numberfield; ?>.<br />
email <?php echo $emailfield; ?>.<br />
message <?php echo $messagefield; ?>.<br />

<?php

//call connection info and connect
include("connect2database.php");



if (!$conn)
  {exit("Connection Failed: " . $conn);}

//Check Connection
if (!$conn)
  { 
  die('Could not connect: ' . mysql_error());
  }
  

//insert new data

//prepare sql string
$sql="INSERT INTO tblContact (DateLeft, Name, Contact, Email, Message) 
VALUES 
('" . date("Y-M-d H:i:s") . "','" . $namefield . "','" . $numberfield . "','" . $emailfield . "','" . $messagefield . "')";
echo $sql . "</br>";

//execute query
mysql_select_db($myDB, $conn);
if (!mysql_query($sql, $conn))
  {
  die('Die Error: ' . mysql_error());
  }
//echo "1 record added";





//close connection
odbc_close($conn);
?>

</body>
</html>

 

but I get on the web page

Welcome 23.

number 34.

email 56.

message 67.

INSERT INTO tblContact (DateLeft, Name, Contact, Email, Message) VALUES ('2009-Apr-04 19:45:44','23','34','56',' 67')

Die Error:

 

now it is not the SQL string because if i copy the echo'd string and paste into SQL query and run it, it adds to the database no problem. so somewhere it is failing to write to the database as I get the DIE ERROR. it cannot be security rights as I am connecting as "sa" so must have all the rights in the world. I set up System ODBD for "MyWeb to log in with "sa" and the ODBD test completes successfully.

 

Please help throw some light on this

 

Thanks

 

None of the mysql_ functions you switched to in the second set of code are going to work with an odbc database connection. You need to use odbc_ functions.

 

Well that makes sense... now that it has been pointed out. in the first set all instructions were prefixed ODBC_ and in the second mysql_. brilliant.

 

I don't suppose you have a list of equivalent odbc commands i will need or point me to a web page. Thanks again PFMaBiSmAd

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.