Jump to content

[SOLVED] something is wrong with this


gwydionwaters

Recommended Posts

now i find this strange, if in the case of the author already existing this works flawlessly but if the author is new there is an error as:

mysql_result() [function.mysql-result]: Unable to jump to row 1 on MySQL result index 4

also it has the same error if i use row 0

the code is this

<?
include("dbase.incl.php");
$Title=$_POST['Title'];
$Author=$_POST['Author'];
$EMail=$_POST['EMail'];
$Type_id=$_POST['Type'];
$Func_id=$_POST['Func'];
$Cost_id=$_POST['Cost'];
$Rating_id=$_POST['Rate'];
$Description=$_POST['Description'];
mysql_connect($hostname,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
$query1 = "SELECT author.Author, author.Author_id FROM author WHERE Author='$Author'";
$result1=mysql_query($query1);
$num=mysql_num_rows($result1);
   // ** the error line is below  **
$Author_id=mysql_result($result1,1,'Author_id');
if ($num =(0)) {
$query2 = "INSERT INTO author.Author, author.EMail VALUES ('$Author', '$EMail')";
mysql_query($query2);
$query3 = "SELECT author.Author_id FROM author WHERE Author = '$Author'";
$result2=mysql_query($query3);
$Author_id=mysql_result($result2,1,'Author_id');
}
$query4 = "INSERT INTO items VALUES ('','$Title','$Cost_id','$Type_id','$Func_id','$Rating_id','$Author_id','$Description')";
mysql_query($query4);
mysql_close();
?>

i also had it so that it was like this

<?
include("dbase.incl.php");
$Title=$_POST['Title'];
$Author=$_POST['Author'];
$EMail=$_POST['EMail'];
$Type_id=$_POST['Type'];
$Func_id=$_POST['Func'];
$Cost_id=$_POST['Cost'];
$Rating_id=$_POST['Rate'];
$Description=$_POST['Description'];
mysql_connect($hostname,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
$query1 = "SELECT author.Author, author.Author_id FROM author WHERE Author='$Author'";
$result1=mysql_query($query1);
$num=mysql_num_rows($result1);
if ($num =(0)) {
$query2 = "INSERT INTO author.Author, author.EMail VALUES ('$Author', '$EMail')";
mysql_query($query2);
$query3 = "SELECT author.Author_id FROM author WHERE Author = '$Author'";
$result2=mysql_query($query3);
$Author_id=mysql_result($result2,1,'Author_id');
} else {
      $Author_id=mysql_result($result1,1,'Author_id');  // ** This was where it was **
}
$query4 = "INSERT INTO items VALUES ('','$Title','$Cost_id','$Type_id','$Func_id','$Rating_id','$Author_id','$Description')";
mysql_query($query4);
mysql_close();
?>

sorry it may be messy, but like i said it works as long as the author already exists in author(table)

Link to comment
https://forums.phpfreaks.com/topic/139063-solved-something-is-wrong-with-this/
Share on other sites

It's the little things (also, use <?php in your code so it is color coded):

 

Original:

<?php
include("dbase.incl.php");
$Title=$_POST['Title'];
$Author=$_POST['Author'];
$EMail=$_POST['EMail'];
$Type_id=$_POST['Type'];
$Func_id=$_POST['Func'];
$Cost_id=$_POST['Cost'];
$Rating_id=$_POST['Rate'];
$Description=$_POST['Description'];
mysql_connect($hostname,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
$query1 = "SELECT author.Author, author.Author_id FROM author WHERE Author='$Author'";
$result1=mysql_query($query1);
$num=mysql_num_rows($result1);
if ($num =(0)) {
   $query2 = "INSERT INTO author.Author, author.EMail VALUES ('$Author', '$EMail')";
   mysql_query($query2);
   $query3 = "SELECT author.Author_id FROM author WHERE Author = '$Author'";
   $result2=mysql_query($query3);
   $Author_id=mysql_result($result2,1,'Author_id');
} else {
      $Author_id=mysql_result($result1,1,'Author_id');  // ** This was where it was **
}
$query4 = "INSERT INTO items VALUES ('','$Title','$Cost_id','$Type_id','$Func_id','$Rating_id','$Author_id','$Description')";
mysql_query($query4);
mysql_close();
?>

 

Fixed:

<?php
include("dbase.incl.php");
$Title=$_POST['Title'];
$Author=$_POST['Author'];
$EMail=$_POST['EMail'];
$Type_id=$_POST['Type'];
$Func_id=$_POST['Func'];
$Cost_id=$_POST['Cost'];
$Rating_id=$_POST['Rate'];
$Description=$_POST['Description'];
mysql_connect($hostname,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
$query1 = "SELECT author.Author, author.Author_id FROM author WHERE Author='$Author'";
$result1=mysql_query($query1);
$num=mysql_num_rows($result1);
if ($num == 0) { // <--- fixed
   $query2 = "INSERT INTO author.Author, author.EMail VALUES ('$Author', '$EMail')";
   mysql_query($query2);
   $query3 = "SELECT author.Author_id FROM author WHERE Author = '$Author'";
   $result2=mysql_query($query3);
   $Author_id=mysql_result($result2,1,'Author_id');
} else {
      $Author_id=mysql_result($result1,1,'Author_id');  // ** This was where it was **
}
$query4 = "INSERT INTO items VALUES ('','$Title','$Cost_id','$Type_id','$Func_id','$Rating_id','$Author_id','$Description')";
mysql_query($query4);
mysql_close();
?>

 

Also, remember that mysql_result(resource,row,column), where column can be the column heading or a number. Example from my book:

 

<?php
//nametable: |pkey| name |
// |----|------|
// Row 0: | 3  | Mark |
// Row 1: | 2  | John |
$result = mysql_query("SELECT * FROM nametable");
var_dump( mysql_result($result, 0) );
var_dump( mysql_result($result, 1) );?>

string(1) "3"

string(1) "2"

<?php
var_dump( mysql_result($result, 0, 1) );
var_dump( mysql_result($result, 1, 'name') );

string(4) "Mark"

string(4) "John"

?>[/code]

thank you so much! :) works great now. at first it didn't and i couldn't figure out why, turns out that my if statements insert into author which was

INSERT INTO author.Author, author.EMail VALUES ('$Author','$EMail');

for some reason wasn't working so i changed it to

INSERT INTO author VALUES ('$Author','$EMail','');

which now works. again thanks for your help.

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.