Newbi --> help with ODBC (acces database)

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Draco_03
Forum Regular
Posts: 577
Joined: Fri Aug 15, 2003 12:25 pm
Location: Montreal, Canada

Newbi --> help with ODBC (acces database)

Post by Draco_03 »

Hi all :)
i'm learning php and some basic db stuff.. like i can read and print any info of my DB. BUT i can't get the insert to work properly..

i'm making lots of test and well it'll go faster if someone helps me :)
i know this is not good .. but i really don t get the insert function AND i absoloutly don t know how to use the WHERE ...

nyways as you can see i m a real noob :) but i m trying...

okay my table name is List, i have 3 field
1-nom
2-phone
3-key (wich is my primary key)

Code: Select all

<table width="752" border="0" align="center" cellpadding="0" cellspacing="0">
  <tr align="center" class="textegris"> 
    <td height="25"><strong>Nom</strong></td>
    <td height="25"><strong>Centre</strong></td>
  </tr>
<?php
$name = $_POST&#1111;"name"]; 
$centre = $_POST&#1111;"centre"]; 

echo ("<tr>");
echo ("<td>$nom</td>"); 
echo ("<td>$centre</td>");
echo ("</tr>");

if (!($connect = odbc_connect("x", "user", "pswd"))) &#123;
echo "connection failed";
&#125;else&#123;
$connect = odbc_connect("x", "xx", "pswd");
$query = "INSERT INTO List ( nom, phone, key) VALUES ('nom', 'centre', '1')";
$queryexe = odbc_do($connect, $Query);
odbc_close($connect);
?>
thx in advance .. :) i appreciate
User avatar
scorphus
Forum Regular
Posts: 589
Joined: Fri May 09, 2003 11:53 pm
Location: Belo Horizonte, Brazil
Contact:

Post by scorphus »

• I will assume that obdc_connect is actualy connecting and openning the right database
• You could check if the query is being correctly performed this way:

Code: Select all

<?php
// ... connect to OBDC
odbc_do($connect, $Query) or die("OBDC Error:" . odbc_errormsg());
// ...
?>
• Are you getting another error? Please post the error message(s) if you get some. Also please post the design of the List table. This will help us help you ;)

Regards,
Scorphus.
User avatar
scorphus
Forum Regular
Posts: 589
Joined: Fri May 09, 2003 11:53 pm
Location: Belo Horizonte, Brazil
Contact:

Post by scorphus »

Oh, now I see! You set your query to variable $query and in the obdc_do function you're using $Query with capital Q. This must be the reason.

Hope it helps,
Scorphus.
Draco_03
Forum Regular
Posts: 577
Joined: Fri Aug 15, 2003 12:25 pm
Location: Montreal, Canada

Post by Draco_03 »

Hey thx for answering...

Yes you were right i did in fact put Q .. but i fixed my prob by doiing that

Code: Select all

<table width="752" border="0" align="center" cellpadding="0" cellspacing="0">
  <tr align="center" class="textegris"> 
    <td height="25"><strong>Nom</strong></td>
    <td height="25"><strong>Phone</strong></td>
  </tr>
<?php
$nom = $_POST&#1111;"nom"]; 
$phone = $_POST&#1111;"phone"]; 
$var = "12";

echo ("<tr>");
echo ("<td><strong>$nom</strong></td>"); 
echo ("<td>$phone</td>");
echo ("</tr>");

$connect = odbc_connect("x", "user", "pswd") or die("Connection failed");
$query = "INSERT INTO List ( nom, phone, key) VALUES ('$nom', '$phone', '$var')";
$queryexe = odbc_do($connect, $query);
odbc_close($connect);
?>
</table>
it worked.. i was happy .. but now i wanted to get the primary key and automaticaly add 1 (if the key is 11, it will put on the next entery 12)
ex : x=12 ----> x++ now x=13 :)
nyways i just don t know why it s not adding value in my DB now :P here s my code (i have no errors on this one)

Code: Select all

<table width="752" border="0" align="center" cellpadding="0" cellspacing="0">
  <tr align="center" class="textegris"> 
    <td height="25"><strong>Nom</strong></td>
    <td height="25"><strong>Phone</strong></td>
  </tr>
<?php
$connect = odbc_connect("x", "user", "pswd") or die("Connection failed");
$query = "SELECT key FROM List";
$queryexe = odbc_do($connect, $query);
while(odbc_fetch_row($queryexe)) 
    &#123; 
    $varkey = odbc_result($queryexe, 1);
	&#125;

odbc_close($connect);
print "$varkey";
$nom = $_POST&#1111;"nom"]; 
$phone = $_POST&#1111;"phone"];
$var = $varkey;
if ($var == $varkey) &#123;
$var++;
&#125;
echo ("<tr>");
echo ("<td><strong>$nom</strong></td>"); 
echo ("<td>$phone</td>");
echo ("</tr>");

$connect = odbc_connect("x", "user", "pswd") or die("Connection failed");
$query = "INSERT INTO List ( nom, phone, key) VALUES ('$nom', '$phone', '$var')";
$queryexe = odbc_do($connect, $query);
odbc_close($connect);
?>
</table>
if you can tell me why its not inserting the value in my Database that would be helpful

thx again :)
Draco_03
Forum Regular
Posts: 577
Joined: Fri Aug 15, 2003 12:25 pm
Location: Montreal, Canada

Post by Draco_03 »

OH i fixed my prob i just changed the

odbc_close($connection);

..wouhou :P

okay well thx :)
Post Reply